Skip to content

Instantly share code, notes, and snippets.

@soofaloofa-zz
Created June 17, 2011 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soofaloofa-zz/1031880 to your computer and use it in GitHub Desktop.
Save soofaloofa-zz/1031880 to your computer and use it in GitHub Desktop.
3. Cons the Magnificent
(defn rember [atom l]
(loop [lat (seq l) res []]
(if lat
(let [f (first lat)]
(if (= f atom)
(next lat)
(recur (next lat) (conj res f))))
res)))
(defn firsts [lol]
(loop [l (seq lol) res []]
(if l
(recur (next l) (conj res (first (first l))))
res)))
(defn insertR [new old lat]
(loop [l (seq lat) res []]
(if l
(if
(= (first l) old) (concat (conj res old new) (next l))
(recur (next l) (conj res (first l))))
res)))
(defn insertL [new old lat]
(loop [l (seq lat) res []]
(if l
(if
(= (first l) old) (concat (conj res new) l)
(recur (next l) (conj res (first l))))
res)))
(defn subst [new old lat]
(loop [l (seq lat) res []]
(if l
(if
(= (first lat) old) (concat res (conj (next lat) new))
(recur (next l) (conj res (first l))))
res)))
(defn subst2 [new o1 o2 lat]
(loop [l (seq lat) res []]
(if l
(if
(or (= (first l) o1) (= (first l) o2)) (concat res (conj (next l) new))
(recur (next l) (conj res (first l))))
res)))
(defn multirember [a lat]
(loop [l (seq lat) res []]
(if l
(if
(= (first l) a) (recur (next l) res)
(recur (next l) (conj res (first l))))
res)))
(defn multiinsertR [new old lat]
(loop [l (seq lat) res []]
(if l
(if
(= (first l) old) (recur (next l) (conj res old new))
(recur (next l) (conj res (first l))))
res)))
(defn multiinsertL [new old lat]
(loop [l (seq lat) res []]
(if l
(if
(= (first l) old) (recur (next l) (conj res new old))
(recur (next l) (conj res (first l))))
res)))
(defn multisubst [new old lat]
(loop [l (seq lat) res []]
(if l
(if
(= (first l) old) (recur (next l) (conj res new))
(recur (next l) (conj res (first l))))
res)))
@soofaloofa-zz
Copy link
Author

Clojure functions for Chapter 3 of The Little Schemer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment