Clojure Exercise Well

I found some nice Clojure exercises .

Basically, practice makes perfect, especially with recursion. Oh yes, I learned recursion taking a course in data structures long ago, and re-learned recursion when learning C. I have never used recursion in production that I can remember. That is not true with Lisp dialects.

There are other ways to remove list duplicates, but I wanted to roll my own on this one. I made two mistakes. One was not having an else for the second if expression. The other was checking for null of new-seq instead of (first new-seq).

(ns repl-test
  (:use clojure.contrib.command-line)
  (:require [clojure.contrib.string :as cstr])
  (:require [clojure.contrib.trace :as ctr])
  (:use clojure-csv.core))

(def d3 [1 2 3 1 4 1 2])

(defn x-in-seq [x temp-seq]
  (if (nil? (some #(= x %) temp-seq))

(defn f1
  (loop [new-seq [] cur-seq in-seq]
    (if (nil? (first cur-seq))
      ;(if-not (nil? (ctr/trace (x-in-seq (first cur-seq) new-seq)))
      (if-not (nil? (x-in-seq (first cur-seq) new-seq))
         (recur (conj new-seq (first cur-seq)) (rest cur-seq))
         (recur new-seq (rest cur-seq))))))

(defn -main
[& args]

(println (f1 d3)))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s