Author | Message |
---|---|
rabbott
Posts: 1649
|
Posted 09:44 Feb 17, 2019 |
Yesterday I suggested the following for Haskell. > data FibElement = I Integer | Fib Integer | Plus deriving Show
You can do something similar in Python using the class Fib: def __str__(self):
def fib_top_down_iter_v2(n, trace=False): (inp, stack) = ([Fib(n)], [])
[Fib(6)] []
[+,Fib(5),Fib(4)] []
[+,Fib(5),+,Fib(3),Fib(2)] []
[+,Fib(5),+,Fib(3),1] []
[+,Fib(5),+,Fib(3)] [1]
[+,Fib(5),+,+,Fib(2),Fib(1)] [1]
[+,Fib(5),+,+,Fib(2),1] [1]
[+,Fib(5),+,+,Fib(2)] [1, 1]
[+,Fib(5),+,+,1] [1, 1]
[+,Fib(5),+,+] [1, 1, 1]
[+,Fib(5),+,2] [1]
[+,Fib(5),+] [2, 1]
[+,Fib(5),3] []
[+,Fib(5)] [3]
[+,+,Fib(4),Fib(3)] [3]
[+,+,Fib(4),+,Fib(2),Fib(1)] [3]
[+,+,Fib(4),+,Fib(2),1] [3]
[+,+,Fib(4),+,Fib(2)] [1, 3]
[+,+,Fib(4),+,1] [1, 3]
[+,+,Fib(4),+] [1, 1, 3]
[+,+,Fib(4),2] [3]
[+,+,Fib(4)] [2, 3]
[+,+,+,Fib(3),Fib(2)] [2, 3]
[+,+,+,Fib(3),1] [2, 3]
[+,+,+,Fib(3)] [1, 2, 3]
[+,+,+,+,Fib(2),Fib(1)] [1, 2, 3]
[+,+,+,+,Fib(2),1] [1, 2, 3]
[+,+,+,+,Fib(2)] [1, 1, 2, 3]
[+,+,+,+,1] [1, 1, 2, 3]
[+,+,+,+] [1, 1, 1, 2, 3]
[+,+,+,2] [1, 2, 3]
[+,+,+] [2, 1, 2, 3]
[+,+,3] [2, 3]
[+,+] [3, 2, 3]
[+,5] [3]
[+] [5, 3]
[8] []
8
Last edited by rabbott at
09:50 Feb 17, 2019.
|
rabbott
Posts: 1649
|
Posted 09:56 Feb 17, 2019 |
To do what is illustrated above, you don't need the
Last edited by rabbott at
10:05 Feb 17, 2019.
|