[Haskell-cafe] memoization

Bulat Ziganshin bulat.ziganshin at gmail.com
Thu Sep 10 08:07:54 EDT 2009

Hello staafmeister,

Thursday, September 10, 2009, 3:54:34 PM, you wrote:

> What do you think about such a function? This function is

a bit of refactoring

-- "global variable" in haskell way
cache = unsafePerformIO $ newIORef M.empty

memo f x = unsafePerformIO$ do
                       m <- readIORef cache
                       case M.lookup x m of
                         Just y -> return y
                         Nothing -> do let res = f x
                                       writeIORef cache $ M.insert x res m
                                       return res

memo2 = curry . memo . uncurry

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list