4 ms·
I will have to look for mutual recursion support (or lack thereof) in clojure. Clojure's loop/recur is a work-around for some issue with the JVM and lack of fi
by DigitalJack 10y ago
I will have to look for mutual recursion support (or lack thereof) in clojure.
Clojure's loop/recur is a work-around for some issue with the JVM and lack of fine grained control over the stack. That said, It's not a simple loop, the keyword "loop" is effectively an anonymous function that gets called by the "recur" keyword, with parameters.
An example:
(loop [iter 1
acc 0]
(if (> iter 10)
(println acc)
(recur (inc iter) (+ acc iter))))
I don't know if that is any different than other lisps or not. In case it's less less than clear, the bracketed portion after the "loop" keyword is an initial binding form (setting iter to 1, and acc to 0), and thereafter iter and acc are just considered parameters.
For self recursion, that seems like a fair compromise. No help at all for mutual recursion though.
- dmichulke 10y agoI never had to use it but I recall there was a 'trampoline' fn for mutual recursion
- smnplk 10y agoExactly. I once read a great article [1] written by Martin Trojer I think, it was about recursion in Clojure. EDIT: [1] https://martintrojer.github.io/clojure/2011/11/20/tail-calls-in-f-clojure-and-scala https://martintrojer.github.io/clojure/2011/11/20/tail-calls...