Compilers usually do not provide the "memoize" parameter, because it is difficult to know where and how the programmer wants to perform the memonization. Memorization is mainly related to the needs of time and space.
Now, if you want to write a function a little differently, then there is a way to separate the definition of the function and the memoization method used.
import Data.RunMemo (Memoizable, runMemo, noMemo) import Data.MemoCombinators as Memo (integral) fibMemoizable :: Memoizable (Integer -> Integer) fibMemoizable recurse = go where go 0 = 1 go 1 = 1 go n = recurse (n - 1) + recurse (n - 2) fastFib :: Integer -> Integer fastFib = runMemo Memo.integral fibMemoizable slowFib :: Integer -> Integer slowFib = runMemo noMemo fibMemoizable
This uses the Luke Palmer data-memocombinators package , as well as my own toy package, runmemo . Note that his meat is the same as what you wrote, except that he calls recurse for recursive calls. Although it can be baked in the compiler, I see no reason, since Haskell is expressive enough to handle this without requiring the compiler to know what we are doing.
Dan burton
source share