Q about tail recursion
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, container)
| > else:
| > container.append(sofar)
| > add(add, inlist, 0, sum)
| > return sum
| Both "recur(recur, ..." and "container.append(..." are tail calls in this
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
* 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