Q about tail recursion

Harald Hanche-Olsen hanche at math.ntnu.no
Sat Feb 26 10:09:15 EST 2000


+ "Tim Peters" <tim_one at email.msn.com>:

| > def faketailadd(inlist):
| >     sum = []
| >     def add(recur,inlist, sofar, container):
| >         if inlist:
| >             recur(recur, inlist[1:], sofar+inlist[0], container)
| >         else:
| >             container.append(sofar)
| >     add(add, inlist, 0, sum)
| >     return sum[0]
| 
| Both "recur(recur, ..." and "container.append(..." are tail calls in this
| "add").

Um, not really, because they are followed by an implicit "return None".

They would become tail calls if preceded by the keyword "return".
This seems to be one of those examples in which more is less.

Not that it mattes really, since Python doesn't optimize tail calls
anyway.

anally-correct-ly y'rs,
-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- "There arises from a bad and unapt formation of words
   a wonderful obstruction to the mind."  - Francis Bacon



More information about the Python-list mailing list