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
http://aspn.activestat
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:
> 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
"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
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 recursio
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,
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 le
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 a
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:
>
> 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
[...]
> >
> 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):
>
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,
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:
> 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
http://www.phyast.pitt.edu/~michele
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
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 ch
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
--
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 i
Kay Schluehr wrote:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
Neat.
Diez
--
http://mail.python.org/mailman/listinfo/python-list
19 matches
Mail list logo