This is my code:
import Data.Function.Memoize
import Debug.Trace
foo :: Int -> Int -> Int
foo a = memoFix fooMemo where
fooMemo f x = a + (trace (show x) cont) where
cont = if x == 0 then 0 else x + f (x - 1)
main = do
print $ foo 0 5
print $ foo 0 5
print $ foo 0 5
I expected it to print:
3
2
1
0
6
6
6
But, instead, it prints this:
3
2
1
0
6
3
2
1
0
6
3
2
1
0
6
In other words, the function is not memoized as I expected it to be. This is probably because each time "foo 0" is called, a new memo-table is created for "foo". How can I force GHC to evaluate "memoFix fooMemo" only once, so that it doesn't create more than one memotable?
Data.Function.Memoize
from?3
as the last argument rather than5
.