I'm trying to recursively reverse a list, but am getting Can only recur from tail position upon run. What does this mean precisely and how can my code be improved so it works?
(defn recursive-reverse [coll]
(loop [coll coll]
(if (< (count coll) 2) '(coll)
(conj (first coll) (recur (rest coll)))
)))
EDIT
Output for Oscar's solution. It works for lists but not vectors?
user=> (= (recursive-reverse [1 2 3 4 5]) (recursive-reverse '(1 2 3 4 5)))
false
user=> (= '(1 2 3 4 5) [1 2 3 4 5])
true
user=> (recursive-reverse [1 2 3 4 5])
[1 2 3 4 5]
user=> (recursive-reverse '(1 2 3 4 5))
(5 4 3 2 1)
conjworks differently for both. It's cheapest to "add" item to the front of a list, and to the end of a vector.conjlooks at the type of collection and adds it in the cheapest way. To be honest this never felt quite right to me, but I'm sure Rich Hickey and the other Clojure devs have thought very thoroughly about it and decided the pros outweigh any cons. - Gert