[Haskell-cafe] memoization
Christopher Howard
christopher.howard at frigidcode.com
Mon Jul 22 10:02:54 CEST 2013
On 07/21/2013 11:52 PM, Chris Wong wrote:
> [.. snipped ..]
>
> A binding is memoized if, ignoring everything after the equals sign,
> it looks like a constant.
>
> In other words, these are memoized:
>
> x = 2
>
> Just x = blah
>
> (x, y) = blah
>
> f = \x -> x + 1
> -- f = ...
>
> and these are not:
>
> f x = x + 1
>
> f (Just x, y) = x + y
>
> If you remove the right-hand side of memoized_fib, you get:
>
> memoized_fib = ...
>
> This looks like a constant. So the value (map fib [0..] !!) is memoized.
>
> If you change that line to
>
> memoized_fib x = map fib [0..] !! x
>
> GHC no longer memoizes it, and it runs much more slowly.
>
> --
> Chris Wong, fixpoint conjurer
> e: lambda.fairy at gmail.com
> w: http://lfairy.github.io/
Thanks. That's very helpful to know. Yet, it seems rather strange and
arbitrary that "f x = ..." and "f = \x -> ..." would be treated in such
a dramatically different manner.
More information about the Haskell-Cafe
mailing list