
I agree with what you meant, but not quite with what you said. To be pedantic:
import Debug.Trace
foo :: Int foo = trace "Foo" (bar 12)
bar :: Int -> Int bar x = trace "Bar" x
main :: IO () main = foo `seq` foo `seq` return ()
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.
baz :: Int -> Int baz x = trace "Baz" (bar x)
correct :: IO () correct = baz 10 `seq` baz 11 `seq` return ()
Though, as you said, call, you probably meant foo was a function, and correct prints "Baz\nBar\nBaz\nBar\n" like you had indicated. But pedantically even the function:
quux :: Int -> Int quux x = trace "Quux" (bar 12) optmain :: IO () optmain = quux 10 `seq` quux 11 `seq` return ()
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.
-Edward Kmett
On Sun, Sep 13, 2009 at 7:45 PM, Mark Wotton
On 14/09/2009, at 9:28 AM, Casey Hawthorne wrote:
Do I have this right? "Remembering" Memoization!
For some applications, a lot of state does not to be saved, since "initialization" functions can be called early, and these functions will "remember" - (memoize) their results when called again, because of lazy evaluation?
You don't get memoisation for free. 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.
Cheers Mark
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe