<div>I agree with what you meant, but not quite with what you said. To be pedantic:</div>
<div> </div>
<div>> import Debug.Trace</div>
<div> </div>
<div>> foo :: Int<br>> foo = trace "Foo" (bar 12)</div>
<div> </div>
<div>> bar :: Int -> Int<br>> bar x = trace "Bar" x</div>
<div> </div>
<div>> main :: IO ()<br>> main = foo `seq` foo `seq` return ()</div>
<div> </div>
<div>main prints "Foo\nBar\n" showing that the bar is only evaluated once, because foo is already evaluated, even though it is referenced twice. So attempting to evaluate foo again just returns the same result.</div>
<div> </div>
<div>
<div>> baz :: Int -> Int<br>> baz x = trace "Baz" (bar x)</div>
<div> </div></div>
<div>> correct :: IO () <br>> correct = baz 10 `seq` baz 11 `seq` return ()</div>
<div> </div>
<div>Though, as you said, call, you probably meant foo was a function, and correct prints "Baz\nBar\nBaz\nBar\n" like you had indicated.</div>
<div> </div>
<div>But pedantically even the function:</div>
<div> </div>
<div>> quux :: Int -> Int<br>> quux x = trace "Quux" (bar 12)
<div> </div>
<div>> optmain :: IO () <br>> optmain = quux 10 `seq` quux 11 `seq` return ()</div></div>
<div> </div>
<div>might print only once if GHC at the optimization level selected recognizes that quux doesn't depend on its argument and rewrote your code with more sharing.</div>
<div> </div>
<div>-Edward Kmett</div>
<div> </div>
<div class="gmail_quote">On Sun, Sep 13, 2009 at 7:45 PM, Mark Wotton <span dir="ltr"><<a href="mailto:mwotton@gmail.com">mwotton@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div class="im"><br>On 14/09/2009, at 9:28 AM, Casey Hawthorne wrote:<br><br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Do I have this right? "Remembering" Memoization!<br><br>For some applications, a lot of state does not to be saved, since<br>
"initialization" functions can be called early, and these functions<br>will "remember" - (memoize) their results when called again, because<br>of lazy evaluation?<br></blockquote><br></div>You don't get memoisation for free.<br>
If you define a variable once in a where block, it's true that you'll evaluate it at most once, but if you repeatedly call a function "foo" that then calls "bar 12" each time, "bar 12" will be evaluated once per "foo" call.<br>
<br>Cheers<br><font color="#888888">Mark</font>
<div>
<div></div>
<div class="h5"><br>_______________________________________________<br>Haskell-Cafe mailing list<br><a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br>