
Tail Recursion Elimination (Guido van Rossum, 2009) - tosh
https://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html
======
zombieprocesses
> Is it really such a big deal to rewrite your function to use a loop? (After
> all TRE only addresses recursion that can easily be replaced by a loop. :-)

Sure. Recursion is iteration. But the beauty of recursion is that it lends
itself to mathematical/algorithmic/etc thinking and is far more elegant and
readable in many situations. Try writing something basic as fib as a recursive
function and then in a loop. One is elegant and the other is kludgy.

Lots of things aren't a "big deal" but it's nice to have. It's why we have so
much syntactic sugar in languages and it's also why so much of functional
concepts are moving to OO space.

~~~
dozzie
> Try writing something basic as fib as a recursive function and then in a
> loop. One is elegant and the other is kludgy.

At the same time, one is slow as hell, because recalculates the very same
value multiple times (unless you return a tuple, in which case it loses its
elegance). You've chosen a bad example.

Also, remember that the opinion comes from a guy who mistakes tuples for lists
with some unrelated property (being immutable), even though he had a few
opportunities to fix his initial misunderstanding. Anything coming from van
Rossum about functional programming should be taken with a grain of salt.

~~~
heavenlyblue
One simple reason to not have tail-call elimination is the "nice stack traces"
that are so useful in Python.

~~~
dozzie
This one reason is somewhat OK, but note that these "nice stack traces" are
not really that useful in functional languages (Erlang being a prominent
example). It's a self-inflicted thing that full stack traces are that useful
in Python.

------
bjoli
Let me copypaste a part of a previous comment I made:

When compiling/transpiling/whatever between languages, I have found that
relying on regular procedure calls and TCO is generally a lot simpler than
having to force the looping facility of one language into the semantics of
another language.

I have written a guile scheme macro that more or less copies racket's for
loops, and there are a lot of subtle things that would make using something
else than tail recursive functions hard to use since I would have to work
around the implicit behaviour of whatever looping facility I am trying to use.

~~~
soegaard
+10

TCO is indispensable, when a language is used as the target language for a
compiler.

