[Haskell-cafe] [newbie question] Memoization automatic in Haskell?

Jonathan Cast 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  
>> yourself:
>> 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
> automatically.

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.

jcc




More information about the Haskell-Cafe mailing list