I\'m trying to relearn Haskell, after many years away and forgetting everything, and I find myself still confused my memoization. In particular, I\'m trying to write a
With
nders :: Int -> Integer
nders n = (map der [0 ..]) !! n
where der 0 = 1
der 1 = 0
der n = (nders (n-2) + nders (n-1)) * toInteger (n-1)
the map der [0..]
part will be recomputed for any application of nders
, especially including the recursive calls in der
.
You can move out the definition of the tabulation so that it doesn't (syntactically) depend on n
, which should do the right thing:
nders :: Int -> Integer
nders = (memoized !!)
where
memoized = map der [0 ..]
der 0 = 1
der 1 = 0
der n = (nders (n-2) + nders (n-1)) * toInteger (n-1)