Functional Programming and python

Piet van Oostrum piet at vanoostrum.org
Tue Oct 1 13:31:33 EDT 2013


Antoon Pardon <antoon.pardon at rece.vub.ac.be> writes:

> Op 30-09-13 20:55, Piet van Oostrum schreef:
>> Franck Ditter <nobody at nowhere.org> writes:
>>
>>> Good approach of FP in Python, but two points make me crazy :
>>> 1. Tail recursion is not optimized. We are in 2013, why ? This is known technology (since 1960).
>>> And don't answer with "good programmers don't use recursion", this is bullshit.
>>
>> Tail recursion optimization throws away valuable stack trace information in case of an error.
>
> This is hardly relevant. Because what are we told to use instead of
> tail calls? We are told to use loops. But when you use a loop the
> stack trace doesn't contain the values of previous runs through
> the loop.
>
> So how valuable is that stack frame information when the proposed
> alternative doesn't produces it either.

This is incorrect. Loops are not always a replacement for tail recursion
optimization. Tail recursion optimization generally means that the last
function call before a return in a function reuses the current stack
frame instead of generating a new one. This is regardless of which
function is called, otherwise mutually recursive calls will not be
optimized. (Unless you do static analysis of all the functions calls
involved, which is very hard or even impossible in python.) A better
name for this process is therefore 'tail call optimization'. So any
function call that is last in its flow will be optimized. This gives a
lot of situations where stack trace information is thrown away, even in
those situations where a loop would not be an alternative and where
optimization would give little benefit. Anyway, deep recursion isn't
used very often in Python, which diminishes the value of tail call
optimization.
-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list