Just a quick reply, others might go into more detail.<br><br><div class="gmail_quote">On 15 October 2010 23:03, Jordan Ellis <span dir="ltr">&lt;<a href="mailto:hookflash@hotmail.com">hookflash@hotmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div>Let&#39;s say I have a function which produces a list of odd integers from 1 to n:<div><br></div><div>f n = filter (odd) [1..n]</div>
<div><br></div><div>...and I have another function that makes a list of all sums of pairs of odd numbers from 1 to n:</div><div><br></div><div>g n = [a + b | a &lt;- (f n), b &lt;- (f n)]</div><div><br></div><div>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?</div>
</div></blockquote><div><br></div><div>No, it doesn&#39;t. Haskell compilers doesn&#39;t do <a href="http://en.wikipedia.org/wiki/Common_subexpression_elimination">CSE</a>.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div>If not, would a &#39;where clause&#39; make any difference? e.g.,</div><div><br></div><div>g n = [a + b | a &lt;- h, b &lt;- h] where h = f n</div></div></blockquote></div><div><br></div>Yes, this is the way to go (or local let bindings).<br>
<br><div>HTH,<br>
<div>Ozgur</div></div>