Q about tail recursion
Tim Peters
tim_one at email.msn.com
Sun Feb 27 04:55:42 EST 2000
[Felix Thibault]
> That implicit return is what I was missing...does the 'no tail-call
> optimizations ever' mean there's no point in doing functions like this:
>
> def fun(seq, sofar=0):
> ...
> this = seq.pop()
> return fun(seq, sofar+this)
>
> instead of like this:
>
> def fun(seq):
> ...
> this = seq.pop()
> return this + fun(seq)
If you're programming in Python, use a loop <0.1 wink>. The 2nd version is
likely to be a tiny bit faster today, simply because it passes one fewer
argument.
> ...
> Does this mean that besides sending a value back to the caller, return
> also says "We're done with this namespace/local scope/frame(?), go back to
> where we got called from," ?
Yes.
>> after-all-this-i'm-still-not-sure-what-the-question-was<wink>-ly y'rs
>> - tim
> Well, then here's the real question on my mind that I've been too
> embarassed to ask till now:
>
> Is there a Python version of this scheme function
Yes.
> that has informative variable names
Yes.
> so I can figure out what it does ?
No <wink>.
> Or just - what does this do:
>
> (define Y
> (lambda (le)
> ((lambda (f) (f f))
> (lambda (f)
> (le (lambda (x) ((f f) x))))))) ?
Nothing to be embarrassed about here! This is difficult stuff at first --
Curry didn't call Y the "paradoxical combinator" for nothing <0.9 wink>.
You can find an excellent explanation, including a Scheme version using
sensible names, here:
http://www.ececs.uc.edu/~franco/C511/html/Scheme/ycomb.html
See any book or paper on the implementation of functional languages via
combinators for a more pragmatic view. Raymond Smullyan's "To Mock A
Mockingbird" is a lovely "general interest" intro.
it's-called-"y"-because-that's-what-everyone-says-the-first-
time-they-see-it<wink>-ly y'rs - tim
More information about the Python-list
mailing list