Clojure Koans - Recursion (defn recursive-reverse [coll] (loop [coll coll acc '() ] (if (= (count coll) 0) acc (recur (rest coll) (cons (first coll) acc)) ) ) )
I struggled with this one for a while - I don’t want to admit it, but honestly even though i have been coding since I was 16 years old - there are still many areas where I am rusty or weak. There is actually a surprising amount of low level concepts operating in a list recursive reverse in clojure. - tail-call recursion - accumulator - loop construct I have some questions about how this “special form” loop<http://clojure.org/special_forms#Special%20Forms--(loop%20%5Bbindings*%20%5D%20exprs*)> operates with recur<http://clojure.org/special_forms#Special%20Forms--(recur%20exprs*)> I think I need to make sure that I always remember the following : - the arity of the recur call must match the arity of the loop form - recur must be in the tail position. I’m not sure how this loop / recur mechanism is implemented in the JVM - I imaging that the binding created by ‘loop’ is associated with registers that are overwritten as part of the call to ‘recur’. The documentation says the bindings are rebound on call of recur in parallel and the execution of recur - is done in order. My questions are - Is recur causing some kind of local jump after each binding? - What happens with the return address? - If recur doesn’t consume stack - what is going on with the return address? - Is the return address always the same for the same recursive call? More importantly - Is there a chapter in Knuth AOC that could help me with this? Any other resources? I feel like I am really missing some context. Am I missing anything obvious, important or insightful? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en