[Haskell-cafe] [newbie question] Memoization automatic in Haskell?
jonathanccast at fastmail.fm
Sat Jan 12 18:35:41 EST 2008
On 12 Jan 2008, at 3:30 PM, David Benbennick wrote:
> On 1/12/08, Henning Thielemann <lemming at henning-thielemann.de> wrote:
>> Caching is not the default, but you can easily code this by
>> Define an array and initialize it with all function values.
>> Because of
>> lazy evaluation the function values are computed only when they are
>> requested and then they persist in the array.
> But how can I implement memoization for a more complicated function?
> For example, perhaps I want to memoize
> f :: String -> Int -> Double -> String -> Bool
> In Python, it's pretty easy to memoize this. How can I do it in
> Haskell? I suspect the only way would involve changing the function
> signature to use IO or ST.
> It would be nice if I could just tell the compiler "I command you to
> memoize this function", and have it produce the required code
You can cache anything using mutable hash tables, as you know, and
googling will find you `function's in Haskell that do this for you.
More information about the Haskell-Cafe