Source

"""Thunk (a.k.a. lazy objects) in PyPy.To run on top of the thunk object space with the following command-line: py.py -o thunk fibonacci.pyThis is a typical Functional Programming Languages demo, computing theFibonacci sequence by using an infinite lazy linked list."""try:from__pypy__importthunk# only available in 'py.py -o thunk'exceptImportError:print__doc__raiseSystemExit(2)# ____________________________________________________________classListNode:def__init__(self,head,tail):self.head=head# the first element of the listself.tail=tail# the sublist of all remaining elementsdefadd_lists(list1,list2):"""Compute the linked-list equivalent of the Python expression [a+b for (a,b) in zip(list1,list2)] """returnListNode(list1.head+list2.head,thunk(add_lists,list1.tail,list2.tail))# 1, 1, 2, 3, 5, 8, 13, 21, 34, ...Fibonacci=ListNode(1,ListNode(1,None))Fibonacci.tail.tail=thunk(add_lists,Fibonacci,Fibonacci.tail)if__name__=='__main__':node=FibonacciwhileTrue:printnode.headnode=node.tail