0
class Node:
def __init__(self, head, tail):
self.head = head
self.tail = tail
self.isEmpty = False
class Empty:
def __init__(self):
self.isEmpty = True
def fold(ls,f,z):
if ls.isEmpty:
return z
else:
return f(ls.head,fold(ls.tail,f,z))
print(fold(Node(4,Node(3,Node(2,Node(1,e)))),lambda x,y:x+y,0)) ## Sum
print(fold(Node(3,Node(2,Node(1,e))),lambda x,y:x*y,1)) ## Multiply
print(fold(Node(3,Node(2,Node(1,e))),lambda x,y:x-y,0)) ## Minus
一切工作正常,除了與lambda的減號打印。我在這裏做錯了什麼?蟒蛇高階函數摺疊列表
該示例列表中的結果應該是「0」(3-2-1)。