[Haskell-cafe] Haskell and "memoization"
daniel.is.fischer at web.de
Tue Dec 15 23:28:04 EST 2009
Am Mittwoch 16 Dezember 2009 05:08:39 schrieb Gregory Crosswhite:
> Haskell does not maintain a cache mapping function calls to their values,
> so if you have some function f and call it with, say, the argument 7 in two
> different places in your code, then it will re-evaluate the function at
> each point. The only time it will not do this is when it can see
> explicitly that the value will be shared, i.e. situations like "g (f 7) (f
> 7)" should only result in one evaluation of f 7 in simple cases, presuming
> the compiler is sufficiently smart.
Not even then, necessarily. And it's not always a good idea.
f k = [1 .. 20^k]
g xs ys = genericLength (ys ++ xs)
Finding out when to share is really hard.
More information about the Haskell-Cafe