in reply to Re^5: Doing "it" only once
in thread Doing "it" only once
This would be a more convincing argument if I knew how to get more accurate timestamping, but none the less, both the empirical evidence and my (shallow) understanding of GHC internals, support the statement I made--every single function in Haskell is automatically memoized by the compiler--though I should have added a note to the effect that this only works for function calls that are not optimised away.Er, no. This probably will sound overly technical, but there is not any (what is generally meant by) memoization going on here. It has to do with the way the parser turns the code into a graph, and representing the same graph in the same way. If you kind of squint, you could think of it as a form of compile time memoization, but again, I think that is stretching the terminology. It's the same way that something like...
...only takes half as long to execute as you might expect (in the face of referential transparency). Try the following code to convince yourself (enter "100000000" at the prompt).let x = very_long_computation in x + x
import System.Time( getClockTime ) add_up 0 acc = acc add_up n acc = add_up (n - 1) (acc + n) time = getClockTime >>= print main = do time print (add_up 10000000 0) time print (add_up 5000000 0) time putStrLn "enter number to add up to..." val <- getLine time print (add_up (read val) 0) time
|
|---|