[issue11011] More functools functions

Jason Baker report at bugs.python.org
Wed Jan 26 04:52:58 CET 2011


Jason Baker <amnorvend at gmail.com> added the comment:

I'm not sure I understand how Raymond's alternative for trampoline works.  Let's take the factorial algorithm from wikipedia's page on tail recursion[1].  I've implemented the tail recursive version of the algorithm in Python using trampoline:

    from functools import trampoline, partial

    def factorial(n):
        def fact(i, acc):
            if i:
                return partial(fact, (i-1), (acc * i))
            else:
                return acc
        return trampoline(fact, n, 1)

    >>> factorial(5)
    120


How would I implement this using Raymond's alternative?

[1] http://en.wikipedia.org/wiki/Tail_call#Example_programs

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11011>
_______________________________________


More information about the Python-bugs-list mailing list