7 ms·
Clojure’s loop expression hits this spot for me. It sets a recursion point to which you can jump using any logic inside the body you want, as long as it is from
by pgorczak 4y ago
Clojure’s loop expression hits this spot for me. It sets a recursion point to which you can jump using any logic inside the body you want, as long as it is from tail position. It’s like a while loop turned into an expression. I haven’t encountered any other way to write iterative expressions whose number of iterations isn’t known at the top (like map and reduce).
- japanman425 4y agofor x in y: yield x Job done
- wging 4y agoTail call optimization can get you that, too. If you've written Scheme and/or gone through SICP you might be familiar with this: you write a recursive function, with the recursive function call as the last thing the function does ('tail-recursion'), and the compiler/runtime is able to optimize those recursive calls out rather than consuming one stack frame of space per call ('tail call optimization'). Clojure has loop/recur at least partially because it doesn't support tail-call optimization. See https://en.wikipedia.org/wiki/Tail_call https://en.wikipedia.org/wiki/Tail_call for more. Or SICP might be a good resource. https://sarabander.github.io/sicp/html/1_002e2.xhtml https://sarabander.github.io/sicp/html/1_002e2.xhtml
- amalgamated_inc 4y agoInterestingly, 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)
- masklinn 4y ago> I haven’t encountered any other way to write iterative expressions whose number of iterations isn’t known at the top (like map and reduce). `unfold`, Rust's `loop`, generators, working tail recursion elimination (the lack of which loop/recur is a workaround for)
- bogdanoff_2 4y agoIs that like using "continue" in most C-syntax languages?
- still_grokking 4y agoOnly if you could pass parameters to "continue" (which you can't).