Tail recursion to while iteration in 2 easy steps

Jussi Piitulainen jpiitula at ling.helsinki.fi
Tue Oct 8 03:11:13 EDT 2013


random832 at fastmail.us writes:

> The entire point of tail call optimization requires not keeping the
> intervening stack frames around, in _any_ form, so as to allow
> arbitrarily deep recursion without ever having the possibility of a
> stack overflow. An implementation which reduced but did not
> eliminate the space used per call would not be worthwhile because it
> would not enable the recursive functional programming patterns that
> mandatory tail call optimization allows.
> 
> You could require that an "optimizable tail call" be made explicit.
> Actually, I suspect it might be possible to do this now, by abusing
> exception handling as a control flow mechanism.

Python code already marks many of the functionally relevant tail calls
with 'return'. It just wants to retain the trace.

Another keyword could be used to indicate that the programmer does not
want a stack frame retained. It's probably too much to suggest 'please
return', but how about 'goto return'?

A tail call is a 'goto that passes arguments', and I think 'goto' is a
keyword already.

(Actually I just wanted to suggest 'please return'. Not seriously.)



More information about the Python-list mailing list