Created
June 17, 2011 17:40
-
-
Save soofaloofa-zz/1031880 to your computer and use it in GitHub Desktop.
3. Cons the Magnificent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clojure functions for Chapter 3 of The Little Schemer.