lambda expressions

Jason Orendorff jason at jorendorff.com
Mon Feb 4 20:00:17 EST 2002


Gerson Kurz wrote:
> Jason Orendorff wrote:
> > Gerson Kurz wrote:
> > > So, I see few limitations in the current implementation of lambda for
> > > the DEDICATED lambda-user.
> >
> > Lack of tail recursion could be a problem.
>
> I'm not really sure - I left university six years ago -, but isn't
>
> WHILE = lambda x,y: BOOL(x()) and (FALSE(y()) or WHILE(x,y))
>
> tail recursive?

Yes.

I should have said "Lack of tail call optimization could be a problem."

For spectators:  the reason tail recursion is important at all is,
any tail call can be compiled into (essentially) a goto.  This allows
functions like WHILE() to loop indefinitely without eating up memory.

In Python, tail calls are not optimized.  So in Python this uses
O(N) memory, where N = the number of times the loop executes.
Eventually you get "RuntimeError: maximum recursion depth exceeded".

A much more complete explanation of tail recursion:
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.1

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list