Q about tail recursion
Felix Thibault
felixt at dicksonstreet.com
Sun Feb 27 04:38:24 EST 2000
At 03:26 2/26/00 -0500, Tim Peters wrote:
>[Felix Thibault]
<deletia>
>> Does this mean that even though there are no return's in faketailadd's
>> add, the function still has to work its way back up from all those
>> recursive calls ?
>
>Perhaps you're missing that Python does no tail-call optimizations ever; or
>perhaps that Python invents a "return None" for you if you don't code an
>explict return yourself.
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)
Moshe Zadka <mzadka at geocities.com> wrote:
>*Every* Python function has a "return". Picture every function ending
>with "return None". If it got there, it returns None. A function never
>"falls off the end".
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," ?
<delete Felix's question about benchmarking, and Tim's answer to use
time.clock() >
>
>after-all-this-i'm-still-not-sure-what-the-question-was<wink>-ly y'rs
> - tim
>
>
>
>--
>http://www.python.org/mailman/listinfo/python-list
>
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 that has informative
variable names so I can figure out what it does ?
Or just - what does this do:
(define Y
(lambda (le)
((lambda (f) (f f))
(lambda (f)
(le (lambda (x) ((f f) x))))))) ?
y'rs ly-unpythonic
Felix-
More information about the Python-list
mailing list