I voted for closing, but the short answer is:
GHC does not perform automatic memoization of functions, and this is probably good because it complicates spatial complexity. In addition, memoirization is not a very solvable problem at all, since it requires that the function argument be comparable for equality, which is actually impossible for all types (e.g. functions).
Haskell has loose semantics. GHC provides a more or less challenge to the cost model. Although the overhead of lazy grades at high levels of optimization is not so bad because of the stringency analyzer.
It is very easy to implement memoization in Haskell using a lazy evaluation. However, be careful in using space.
fib' :: (Integer -> Integer) -> Integer -> Integer fib' f 0 = 0 fib' f 1 = 1 fib' fn | n > 1 = (f (n - 1)) + ((f (n - 2)) slow_fib :: Integer -> Integer slow_fib = fib' slow_fib fibs :: [Integer] fibs = map (fib' memo_fib) [0..] memo_fib :: Integer -> Integer memo_fib n = fibs !! n
Actually it is not so fast, and it is a leak of space, but it captures the general idea. You can learn more about the Haskell wiki .
source share