Duncan Booth wrote:
My other problem with this is that the decorator is very fragile although
this may be fixable
This version should be more robust against exceptions:
class tail_recursive(object):
tail_recursive decorator based on Kay Schluehr's recipe
[...]
I'm not convinced by this. You have to recognise that the function is using
tail recursion, and then you have to modify the code to know that it is
using tail recursion. This is not always trivial. For example, the given
example is:
@tail_recursion
def factorial(n, acc=1):
Tim N. van der Leeuw wrote:
I don't know why it wouldn't work this way, or why it isn't
tail-recursion?
From the google page do define: tail recursion
I tried the tail_recursion decorator from the cookbook-recipe with both
definitions of factorial, and I tried both definitions of the
Hi Michele,
I'm sorry, but you misunderstood me.
There are two definitions of the factorial() function, one given by the
OP and the other given by Duncan.
I tested both factorial() definitions with, and without the
tail_recursion decorator (the version of the OP). So I had 4
factorial-functions
Tim N. van der Leeuw wrote:
The other thing I do not understand, due to my limited understanding of
what is tail-recursion: factorial2 (Duncan's definition) is not proper
tail-recursion. Why not? How does it differ from 'real' tail recursion?
Tail recursion is when a function calls itself and
Duncan Booth wrote:
Tim N. van der Leeuw wrote:
[...]
@tail_recursion
def factorial2(n):
# do the stuff
pass
your 'do the stuff' actually had an erroneous call to 'factorial'. If you
are going to rename the function you have to rename the recursive calls as
well. (At least,
Duncan Booth wrote:
The decorator also fails for functions which are tail-recursive but which
contain other non-tail recursive calls within themselves. For example I
would be pretty sure you couldn't write a working implementation of
Ackermann's function using the decorator:
def Ack(M, N):
Duncan Booth [EMAIL PROTECTED] writes:
Tim N. van der Leeuw wrote:
The other thing I do not understand, due to my limited understanding of
what is tail-recursion: factorial2 (Duncan's definition) is not proper
tail-recursion. Why not? How does it differ from 'real' tail recursion?
Alexander Schmolck [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
Duncan Booth [EMAIL PROTECTED] writes:
Tail recursion is when a function calls itself and then immediately
returns
the result of that call as its own result.
Which means that the value returned by the base case is
Kay Schluehr wrote:
Duncan Booth wrote:
The decorator also fails for functions which are tail-recursive but
which contain other non-tail recursive calls within themselves. For
example I would be pretty sure you couldn't write a working
implementation of Ackermann's function using the
Your examples are not tail recursive because an extra step is needed
before returning from the function call and that step cannot be thrown
away!
Alexander Schmolck [EMAIL PROTECTED] wrote:
def even(n):
return n == 0 or not odd(n-1)
def odd(n):
return n == 1 or not even(n-1)
--
Kay Schluehr wrote:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
Neat.
Diez
--
http://mail.python.org/mailman/listinfo/python-list
Diez B. Roggisch wrote:
Kay Schluehr wrote:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
Neat.
Diez
Hi Diez,
for all those who already copied and pasted the original solution and
played with it I apologize for radical changes in the latest version (
the recipe is on
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
--
http://mail.python.org/mailman/listinfo/python-list
Kay Schluehr wrote:
Diez B. Roggisch wrote:
Kay Schluehr wrote:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
Neat.
Diez
Hi Diez,
for all those who already copied and pasted the original solution and
played with it I apologize for radical changes in the latest
Kay Schluehr wrote:
for all those who already copied and pasted the original solution and
played with it I apologize for radical changes in the latest version (
the recipe is on version 1.5 now ! ). The latest implementation is
again a lot faster than the previous one. It does not only get rid
Michele Simionato wrote:
Using my decorator module 'tail_recursive' can even be turned in a
signature-preserving
decorator. I think I will add this great example to the documentation
of the next version
of decorator.py!
Michele Simionato
Done: see
Em Ter, 2006-05-09 às 23:30 -0700, Kay Schluehr escreveu:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
Is it thread safe?
--
Felipe.
--
http://mail.python.org/mailman/listinfo/python-list
Michele Simionato wrote:
CONTINUE = object() # sentinel value returned by iterfunc
def tail_recursive(func):
tail_recursive decorator based on Kay Schluehr's recipe
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
var = dict(in_loop=False, cont=True,
19 matches
Mail list logo