GHC does not memoize functions.
It does, however, compute any given expression in the code at most once per time that its surrounding lambda-expression is entered, or at most once ever if it is at top level. Determining where the lambda-expressions are can be a little tricky when you use syntactic sugar like in your example, so let's convert these to equivalent desugared syntax:
m1' = (!!) (filter odd [1..]) -- NB: See below!
m2' = \n -> (!!) (filter odd [1..]) n
(Note: The Haskell 98 report actually describes a left operator section like (a %)
as equivalent to \b -> (%) a b
, but GHC desugars it to (%) a
. These are technically different because they can be distinguished by seq
. I think I might have submitted a GHC Trac ticket about this.)
Given this, you can see that in m1'
, the expression filter odd [1..]
is not contained in any lambda-expression, so it will only be computed once per run of your program, while in m2'
, filter odd [1..]
will be computed each time the lambda-expression is entered, i.e., on each call of m2'
. That explains the difference in timing you are seeing.
Actually, some versions of GHC, with certain optimization options, will share more values than the above description indicates. This can be problematic in some situations. For example, consider the function
f = \x -> let y = [1..30000000] in foldl' (+) 0 (y ++ [x])
GHC might notice that y
does not depend on x
and rewrite the function to
f = let y = [1..30000000] in \x -> foldl' (+) 0 (y ++ [x])
In this case, the new version is much less efficient because it will have to read about 1 GB from memory where y
is stored, while the original version would run in constant space and fit in the processor's cache. In fact, under GHC 6.12.1, the function f
is almost twice as fast when compiled without optimizations than it is compiled with -O2
.