[Python-ideas] Idea: Compressing the stack on the fly

Ned Batchelder ned at nedbatchelder.com
Mon May 27 15:52:29 CEST 2013


On 5/27/2013 9:30 AM, Chris Angelico wrote:
> On Mon, May 27, 2013 at 9:01 PM, Joao S. O. Bueno <jsbueno at python.org.br> wrote:
>> I have a blog post with such a toy - nevertheless, it is just a toy.
>> (If ther ewas soem small problem that could be elegantly approached
>> in this fashion but not interactively, it could be used in production
>> though)
> What can tail recursion do that can't be done by reassigning to the
> function parameters and 'goto' back to the top? Or, in the absence of
> an actual goto, a construct like this:
>
> def tail_recursive_function(some_arg):
>    while True:
>      # ... code
>      if call_self:
>        # return tail_recursive_function(some_other_arg)
>        some_arg = some_other_arg
>        continue
>      # ... more code
>      # falling off the end:
>      break
>
> which basically amounts to a goto but using extra keywords to avoid
> the one that people hate. Is there any fundamental difference? I've
> never understood there to be any, but I'm only one, and possibly I'm
> wrong.

That style can't handle mutually recursive procedures, or the extreme 
case:  a state machine implemented with N functions, each of which calls 
the next state function at the end.  Tail-call elimination isn't simply 
about noticing recursive calls.  It's about noticing that a function 
ends with a function call, and not burning another stack frame in the 
process.

--Ned.

>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list