3 ms·
Not sure Clojure can do all the recursion stuff (TCO), which would be good to have in a language, when working through SICP exercises. I would not recommend it
by zelphirkalt 6y ago
Not sure Clojure can do all the recursion stuff (TCO), which would be good to have in a language, when working through SICP exercises. I would not recommend it for SICP. Better start with a Scheme, to have the least amount of friction.
- fulafel 6y agoYou can trivially use recur for self-recursion: call (recur x) instead of (my-fn x) inside your function. (There's also trampoline for mutual recursion cases)
- zelphirkalt 6y agoIs trampolining automatic, or does it have its own syntax as well? Even a little thing like (recur ...) could be a tiny amount of friction. But good to read, that the usual cases are apparently handled well in the language.
- fulafel 6y agoThe builtin helper function is called trampoline, it's a higher order function that relies on the mutually recursive functions returning "trampolines" or closures that do the desired subsequent call. The trampoline pattern is an old trick that can also be imlemented in other languages to avoid stack consumption in mutually recursive calls.
- zelphirkalt 6y agoI know what trampolining does, thank you. That's unfortunate, that it is an additional function call, which needs to be explicitly written out. I'd guess such is necessary ultimately, because of limitations of the JVM and its limitations regarding recursion.