[Haskell-cafe] Haskell and "memoization"

Daniel Fischer daniel.is.fischer at web.de
Wed Dec 16 01:51:58 EST 2009


Am Mittwoch 16 Dezember 2009 07:22:42 schrieb Gregory Crosswhite:
> #3 is true for Haskell, it's just that when your function call appears in
> two different places, it is counted as two different expressions.  Each
> separate expression will only be evaluated once, though.  This is what is
> really meant, since the alternative --- i.e., no function ever being called
> more than once for a given set of arguments --- is way too cumbersome to be
> worth doing in practice for any language.
>
> Laziness really means that if you have, say,
>
> 	f x = (g 7) + x
>
> then g 7 need only be evaluated at the first call to f, and then after that
> it can be cached.  In some circumstances, if we had
>
> 	f x = (g 7) + x
> 	h x = (g 7) * x
>
> Then maybe the compiler would decide not only to evaluate each (g 7)
> expression once, but also that the two expression should be merged into
> references to a single shared expression.  However, this is not required
> for laziness;  the only requirement there is that each expression
> separately only be evaluated once.

And, strictly speaking, Haskell is non-strict, not lazy.
Thus, if an implementation decides to evaluate g 7 thrice in

f x = (x,x,x)

r = f (g 7)

it doesn't violate the specs.

>
> Cheers,
> Greg



More information about the Haskell-Cafe mailing list