4 ms·
Interestingly, I almost prefer Clojure's `recur` semantically. Means you don't have to change the function name twice if you rename it, and it's hard to miss th
by amalgamated_inc 4y ago
Interestingly, I almost prefer Clojure's `recur` semantically. Means you don't have to change the function name twice if you rename it, and it's hard to miss that you're recursing.
- masklinn 4y agoIf that's your worry then you can probably use the site's namesake. Though simple recursion is generally easy to spot.
- amalgamated_inc 4y agoWhich site's namesake? Hacker News?
- masklinn 4y agoThe Y combinator.
- amalgamated_inc 4y agoOh is this some Common Lisp thing? Never done it.
- masklinn 4y agoIt's much older, it's lambda calculus stuff. It's a way to implement recursion in a language which doesn't have recursive functions (but for some reason does have first-class functions). However it allows making anonymous functions recurse as well.
- wging 4y agoThose are cool properties. Another one is that you get a compilation error if your recursive call isn't in the tail position (and thus would actually grow the stack when you thought it didn't). One thing I don't think you can do with loop/recur, though, is optimize more complicated bits of recursion than a single function that calls itself. I.e. imagine a recursive call pattern that goes like f -> g -> f -> g -> ... (edit: I'm pretty sure this is why trampoline exists, though I've never really played with it... https://clojuredocs.org/clojure.core/trampoline https://clojuredocs.org/clojure.core/trampoline)
- 613style 4y agoIt's also nice to get an error when you `recur` from a non-tail position rather than the function just quietly becoming truly recursive.
- kazinator 4y agoIf you don't have to change a function's name twice when you rename it, that implies it is not called anywhere. :)