# pypy / demo / fibonacci.py

The branch 'array_equal' does not exist.
 ```Armin Rigo d254c64 2005-05-18 Armin Rigo 15259dd 2007-02-08 Armin Rigo d254c64 2005-05-18 Carl Friedrich B… 21f8044 2007-05-07 Carl Friedrich B… 23b5a2d 2006-11-11 Armin Rigo d254c64 2005-05-18 ``` ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43``` ```""" 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.py This is a typical Functional Programming Languages demo, computing the Fibonacci sequence by using an infinite lazy linked list. """ try: from __pypy__ import thunk # only available in 'py.py -o thunk' except ImportError: print __doc__ raise SystemExit(2) # ____________________________________________________________ class ListNode: def __init__(self, head, tail): self.head = head # the first element of the list self.tail = tail # the sublist of all remaining elements def add_lists(list1, list2): """Compute the linked-list equivalent of the Python expression [a+b for (a,b) in zip(list1,list2)] """ return ListNode(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 = Fibonacci while True: print node.head node = node.tail ```