I am trying to understand Haskell realization of memoization , but I don\'t get how it works:
memoized_fib :: Int -> Integer
memoized_fib = (map fib [0..]
Maybe it's clearer written it as:
memoized_fib n = (map fib [0..]) !! n
so it's just taking the n
th element from the list, and the list is evaluated lazily.
This operator section stuff is exactly the same as normal partial application, but for infix operators. In fact, if we write the same form with a regular function instead of the !!
infix operator, see how it looks:
import Data.List (genericIndex)
memoized_fib :: Int -> Integer
memoized_fib = genericIndex (map fib [0..])
where fib 0 = 0
fib 1 = 1
fib n = memoized_fib(n - 2) + memoized_fib(n - 1)