Fibonacci by memo tree
"... trees are defined [...] and used as memo-structures to store the results of functions so that later calls can access them quickly without recomputation. In general, one or more functions and structures are defined using mutual recursion. ..."[All93].
1:[1] . . . . . . . . 2:[1] 3:[2] . . . . . . . . . . . . 4:[3] 5:[5] 6:[8] 7:[13] . . . . . . . . . . . . . . . . 8:[21] etc.
Inside 'fib', the mutually recursive data-structure 'fibtree' and functions 'build' and 'lookup' do the work:
Try 'fib 8' versus 'fib 8 + fib 8' say and note how many 'evals' each expression takes.
There are more λ-calculus examples here.
References
- [All89] Lloyd Allison,
'Circular Programs and Self-Referential Structures',
Software Practice & Experience, 19(2), pp.99-109,
doi:10.1002/spe.4380190202,
arxiv:2403.01866,
February 1989.
- [All93] Lloyd Allison, 'Applications of Recursively Defined Data Structures', Australian Computer Journal, 25(1), pp.14-20, arxiv:2206.12795, 1993.
- And other publications.
- [All93] Lloyd Allison, 'Applications of Recursively Defined Data Structures', Australian Computer Journal, 25(1), pp.14-20, arxiv:2206.12795, 1993.