
Hi, Am Freitag, den 04.12.2009, 10:36 +0000 schrieb Neil Brown:
But let's say you have:
g x y = f x y * f x y
Now the compiler (i.e. at compile-time) can do some magic. It can spot the common expression and know the result of f x y must be the same both times, so it can convert to:
g x y = let z = f x y in z * z
Now, the Haskell run-time will evaluate f x y once, store the result in z, and use it twice. That's how it can use commonalities in your code and avoid multiple evaluations of the same function call, which I *think* was your question.
Note that although the compiler _could_ do this transformation, it does not actually do it because of some unwanted subtleties: http://www.haskell.org/haskellwiki/GHC:FAQ#Does_GHC_do_common_subexpression_... (I was a bit disappointed when I found out about this, after first hearing how much great optimization a haskell compiler _could_ do, but that’s reality.) Greetings, Joachim -- Joachim "nomeata" Breitner mail: mail@joachim-breitner.de | ICQ# 74513189 | GPG-Key: 4743206C JID: nomeata@joachim-breitner.de | http://www.joachim-breitner.de/ Debian Developer: nomeata@debian.org