Not sure what exactly to google for this question, so I'll post it directly to SO:
- Variables in Haskell are immutable
- Pure functions should result in same values for same arguments
From these two points it's possible to deduce that if you call somePureFunc somevar1 somevar2
in your code twice, it only makes sense to compute the value during the first call. The resulting value can be stored in some sort of a giant hash table (or something like that) and looked up during subsequent calls to the function. I have two questions:
- Does GHC actually do this kind of optimization?
- If it does, what is the behaviour in the case when it's actually cheaper to repeat the computation than to look up the results?
Thanks.