
Just a quick reply, others might go into more detail.
On 15 October 2010 23:03, Jordan Ellis
Let's say I have a function which produces a list of odd integers from 1 to n:
f n = filter (odd) [1..n]
...and I have another function that makes a list of all sums of pairs of odd numbers from 1 to n:
g n = [a + b | a <- (f n), b <- (f n)]
I assume that in most languages, (f n) would get called twice (once for a, and once for b), but what happens in Haskell? Does GHC recognize that, since f is a pure function, it only has to be evaluated once for a given n?
No, it doesn't. Haskell compilers doesn't do CSEhttp://en.wikipedia.org/wiki/Common_subexpression_elimination .
If not, would a 'where clause' make any difference? e.g.,
g n = [a + b | a <- h, b <- h] where h = f n
Yes, this is the way to go (or local let bindings). HTH, Ozgur