Given this function :
e :: Integer -> Integer -> Integer
e _ 1 = 1
e n m
|n == m = e n (n-1)
|otherwise = e (n-1) m + e n (m-1)
If I run it in GHCI, the first evaluation has a huge time e 20 1 takes ~10 seconds, but after that calls to even larger numbers are far reduced, ~0.25 seconds. I assume there is some kind of caching happening?
I kind of see why it explodes because each call to e n m makes two more calls, which then each make two calls, etc. so the number of evaluations eventually gets very large, but a lot of them are just the same reference.
Is there anyway to make Haskell realize this, like some kind of automatic lookup table?
memoizationand see many strategies. Be sure you compile your program instead of time it in the interpreter when you care about performance.e 20 1which ought to reduce immediately. Try something likeprint "hello"-- does that also take a long time if it's the first thing you do?