I am working on some Lisp exercises using Clojure. I am trying to work these exercises without taking advantage of vectors and some Clojure functions.
This function
(defn rev-seq
[s1]
(concat (pop s1) (list (peek s1))))
puts the first element of a list at the end. I want to call this function as many times as it takes to reverse the list (without calling Clojure’s reverse function).
I am not sure what to use in its place. I have experimented with map, apply, and repeat with no success. I would rather have a way to think differently about this than a straight answer, but I am not asking for a discussion.
Firstly, I think you’ll need to convert rev-seq to use
first/restrather thanpeek/popif you want to work on general sequences – at leas in Clojure 1.4peek/popseems to require a PersistentStack:Then you should probably note that applying this function repeatedly will “cycle” a list rather than reversing it. You can see that if you look at the result of a small number of applications using
iterate:An option that would work is to reverse with a recursive function:
Or alternatively you can do a reverse using the technique in clojure.core:
Hope this gives you some ideas!