Clojure Exercise WellPosted: August 25, 2011
I found some nice Clojure exercises http://www.cis.upenn.edu/~matuszek/cis554-2010/Assignments/clojure-01-exercises.html .
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 (:gen-class) (: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)) x)) (defn f1 [in-seq] (loop [new-seq  cur-seq in-seq] (if (nil? (first cur-seq)) new-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)))