Re[Haskell-cafe] [2]: memoization

staafmeister g.c.stavenga at uu.nl
Thu Sep 10 08:23:26 EDT 2009



Hi Bulat,


Bulat Ziganshin-2 wrote:
> 
> 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
> 

This doesn't work and is exactly what I'm afraid the compiler is going to
do. Cache needs to
be associated with the function f.

Otherwise one would get conflicts

Greetings
-- 
View this message in context: http://www.nabble.com/memoization-tp25306687p25382341.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list