Versatile intoPosted: October 9, 2012
Given a Clojure list
(defn test-list '(a b c d))
and working through 99 Lisp problems and the particular problem of reversing a simple list, I’ve struggled to avoid using recursion and Clojure’s reverse function. I am working through those problems to obtain more familiarity and confidence in working with sequences.
The following sample will move one element to the end, but I want to keep going until the end of the list.
(defn rev-seq [s1] (concat (pop s1) (list (peek s1)))) (b c d a)
(repeat (count test-list) (rev-seq-elem test-list)) ((b c d a) (b c d a) (b c d a) (b c d a)) repl-test.core=>
did not help either.
And then, I remembered
which performs the accumulation and reversal all very nicely.
repl-test.core=> (into () test-list) (d c b a)