(defn copy-sequence-1 [sq]
(loop [src sq, dest ()]
(if (seq src)
(recur
(rest src)
(cons (first src) dest))
(reverse dest))))
You can do this:
(defn copy-sequence-2 [sq]
(loop [src sq, dest []]
(if (seq src)
(recur
(rest src)
(conj dest (first src)))
dest)))
You get a vector back but that's not a problem. In fact we read "in idiomatic Clojure code lists are used almost exclusively to represent code forms" (p.90) - for data rather than code "lists rarely offer any value over vectors".
No comments:
Post a Comment