Re: New tail recursion decorator

2006-05-15 Thread Michele Simionato
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

Re: New tail recursion decorator

2006-05-12 Thread Tim N. van der Leeuw
[...] 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):

Re: New tail recursion decorator

2006-05-12 Thread Michele Simionato
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

Re: New tail recursion decorator

2006-05-12 Thread Tim N. van der Leeuw
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

Re: New tail recursion decorator

2006-05-12 Thread Duncan Booth
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

Re: New tail recursion decorator

2006-05-12 Thread Tim N. van der Leeuw
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,

Re: New tail recursion decorator

2006-05-12 Thread Kay Schluehr
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):

Re: New tail recursion decorator

2006-05-12 Thread Alexander Schmolck
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?

Re: New tail recursion decorator

2006-05-12 Thread Terry Reedy
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

Re: New tail recursion decorator

2006-05-12 Thread Duncan Booth
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

Re: New tail recursion decorator

2006-05-12 Thread Casey Hawthorne
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) --

Re: New tail recursion decorator

2006-05-10 Thread Diez B. Roggisch
Kay Schluehr wrote: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691 Neat. Diez -- http://mail.python.org/mailman/listinfo/python-list

Re: New tail recursion decorator

2006-05-10 Thread Kay Schluehr
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

New tail recursion decorator

2006-05-10 Thread Kay Schluehr
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691 -- http://mail.python.org/mailman/listinfo/python-list

Re: New tail recursion decorator

2006-05-10 Thread Duncan Booth
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

Re: New tail recursion decorator

2006-05-10 Thread Michele Simionato
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

Re: New tail recursion decorator

2006-05-10 Thread Michele Simionato
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

Re: New tail recursion decorator

2006-05-10 Thread Felipe Almeida Lessa
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

Re: New tail recursion decorator

2006-05-10 Thread Carl Banks
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,