Baba wrote:
On Aug 21, 7:37 am, John Nagle <na...@animats.com> wrote:
On 8/20/2010 1:17 PM, John Bokma wrote:
I think you mean tail recursion optimization / elimination.
Python does tail recursion:
Not very well.
def cnt(n) :
if n > 0 :
cnt(n-1)
Hi John
I'm intrigued by this example. Is there something missing in the code?
When i run it i get: <function cnt at 0x02B2FE70>
I suppose it is meant to print a sequence of numbers from n down to
zero?
re tail recursion, on wiki i found:
"With tail recursion, there is no need to remember the place we are
calling from—instead, we can leave the stack alone, and the newly
called function will return its result directly to the original
caller. Converting a call to a branch or jump in such a case is called
a tail call optimization. "
not sure i understand that...
is this bit of theory applicable to your cnt function above?
tnx
Baba
Juding from your output, you didn't call the function, you just named
it. To call it you needed to use parentheses, type something like
cnt(5)
The function is a do-nothing function. It makes no calculations, returns
no result. Just illustrating recursion in the simplest way.
Tail call optimization would work fine on that function. CPython doesn't
do such optimization, however. If it did, it would convert the call at
the end to a decrement and a jump. The net effect would be something like:
def cnt(n) :
while True:
if n > 0:
n = n-1
continue
break
Clearly that could be optimized as well. Maybe a great optimizer could
turn it into:
def cnt(n):
pass
DaveA
--
http://mail.python.org/mailman/listinfo/python-list