reduce versus recursion? what am I missing...
Warren Postma
embed at geocities.com
Fri Apr 7 11:57:37 EDT 2000
# I am trying to figure out why function d (recursive) and e (reduce)
# DON'T do the SAME thing:
def d(*tup):
"why doesn't this work?"
if len(tup)>1:
return d(tup[1:])+abs(tup[0]-tup[1])
else:
return 0
l = (1,3,5,7,9)
def e(*tup):
"it works"
return reduce(lambda x,y:abs(x-y),tup,0)
print "--- test:"
print "recursion: ",apply(d,l)
print "reduce: ",apply(e,l)
#---
#Warren
#
# output:
#--- test:
#recursion: 2 ( appears not to actually be recursing more than 1
level!!!???)
#reduce: 5
More information about the Python-list
mailing list