[Haskell-cafe] Memoization

Felipe Almeida Lessa felipe.lessa at gmail.com
Sun May 27 00:57:48 EDT 2007


On 5/27/07, Stefan O'Rear <stefanor at cox.net> wrote:
> memofix :: ((a -> b) -> (a -> b)) -> a -> b
> memofix ff = let g = memoize (ff g) in g
>
> fib = memofix $ \fib k -> case k of
>         0 -> 0
>         1 -> 1
>         n -> fib (n-1) + fib (n-2)

But this way you miss pattern matching and guards? How would you write
something like:

ack = curry (memoize a) where
  a (0,n)                  = n + 1
  a (m,0)                  = ack (m-1) 1
  a (m,n) | m < 0 || n < 0 = error "ack of negative integer"
          | otherwise      = let inner = ack m (n-1)
                             in  ack (m-1) inner


-- 
Felipe.


More information about the Haskell-Cafe mailing list