Haskell: Why does this work -- an example of memoization?
dynamic-programming, haskell, lazy-evaluation, memoization
Solution
`memoized_fib` is a CAF, which is as good as a literal constant in avoiding creation of new stuff. No variables ⇒ no things to bind new stuff to ⇒ no creation of new stuff (in simplified terms).
Problem
Hi I am looking at this example from Memoization: ``` memoized_fib :: Int -> Integer memoized_fib = (map fib [0 ..] !!) where fib 0 = 0 fib 1 = 1 fib n = memoized_fib (n-2) + memoized_fib (n-1) ``` I am just wondering why this even work, since for me if you call `memoized_fib(n-2)` then you are "creating" a new list and doing things with it and after your return from it the list containing partial result would be gone? So `memorized_fib(n-1)` won't benefit from it at all?