[Haskell-beginners] Newbie performance question

Jordan Ellis hookflash at hotmail.com
Fri Oct 15 18:03:42 EDT 2010


I'm trying (really trying!) to get my head around Haskell, but one (among many) significant stumbling blocks for me so far has been trying to figure out how my code is actually going to perform. Here's a contrived example of the sort of thing that confuses me. 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? If not, would a 'where clause' make any difference? e.g.,
g n = [a + b | a <- h, b <- h] where h = f n
Thanks. 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20101015/81e7d06b/attachment.html


More information about the Beginners mailing list