[Haskell-beginners] Newbie performance question

Ozgur Akgun ozgurakgun at gmail.com
Fri Oct 15 18:26:15 EDT 2010


Just a quick reply, others might go into more detail.

On 15 October 2010 23:03, Jordan Ellis <hookflash at hotmail.com> wrote:

> 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
CSE<http://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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20101015/ad2bc41b/attachment.html


More information about the Beginners mailing list