Possibly Pythonic Tail Call Optimization (TCO/TRE)

Antoon Pardon antoon.pardon at rece.vub.ac.be
Mon Jul 13 15:55:50 CEST 2015

On 07/13/2015 02:34 PM, Chris Angelico wrote:
>>> Warping your code around a recursive solution
>>> to make it into a perfect tail call usually means making it look a lot
>>> less impressive; for instance,
>> And sometimes your problem is very easily solved by a number of functions
>> that tail call each other but in python you will need to warp your code
>> around an iterative solution, in order to avoid the stack limit.
> Example, please? In all my Python programming, I've never hit the
> stack limit outside of deliberate playing around (or accidental
> infinite recursion, which means it's doing its job).

I have had to process text coming from a network connection. The data
was mosly unstructered text with some lines that could be used as markers
to proceed through some kind of state machine that indicated how the
following lines needed to be processed. That was easiest written by having
a function for each "state" that would process the next lines until
a marker line was encountered and then call the function corresponding
with the next state. However since a connection could stay active for
days, sooner or later a stack limit would occur if done the natural
recursive way.

>> It seems warping your code is only seen as a problem when going in the
>> functional direction. Going into the iterative direction may take all
>> the warping that is needed, without comment.
> That's because I've never seen code that warps more for iteration than
> it does for programming in general. Again, example please?

And since when is your experience the measure stick for what other people
encounter as problems?

More information about the Python-list mailing list