4 ms·
This is the same as recur in Clojure. It's not general TCO, though, which is much more powerful. I do think it's a shame that Python doesn't have general TCO.
by cup-of-tea 9y ago
This is the same as recur in Clojure. It's not general TCO, though, which is much more powerful.
I do think it's a shame that Python doesn't have general TCO. It's said to be unpythonic because it means there will be two ways to do things. But some things are so easily expressed as a recursion but require considerable thought to be turned into a loop.
- e12e 9y ago> But some things are so easily expressed as a recursion but require considerable thought to be turned into a loop. Do you have some examples of problem+solutions where tco works fine (in a language with tco) - but the manual translation is hard(ish)? I wonder in part after reading the Julia thread on tco - and difficulties with providing guarantees in the general case with tco: https://github.com/JuliaLang/julia/issues/4964 https://github.com/JuliaLang/julia/issues/4964
- lysium 9y agoUsually, I implement state machines with mutually tail recursive functions. Each function represents one state.
- e12e 9y agoRight. The general rewrite would be a loop with a switch and state functions that returned a state? (i was going to say state functions that called back to a step function, but I guess that'd still build a call stack).
- jvandonsel 9y agoThe nice thing about recur in Clojure is that it won't even compile if the call isn't in the tail position. I've inadvertently made a code change that moved the recur call out of the tail position and the error became immediately obvious. With TCO you might not even notice until your stack blows up on a deep nesting.
- dragonwriter 9y ago> I do think it's a shame that Python doesn't have general TCO. It's said to be unpythonic because it means there will be two ways to do things. The usual complaint I hear is about stack traces, not “two ways to do things”, which Python rather often provides anyway.