Frank Poettgen <Frank.Poett...@Post.RxWyTxH-AyAzCyHzEyN.de> schrieb:
> Assume that the evaluation of a function 'expensive' takes a lot of > time. Hence I want to avoid unnecessary evaluations of 'expensive'.
> expensive :: Int -> (Int,Int) > expensive x = (x,x)
> f :: Int -> Int > f x = fst (expensive x) + snd (expensive x)
> Will 'expensive x' be evaluated twice to compute 'f x'?
That will depend on your compiler. Some compilers do common subexpression elimination, others don't. The reason is that this transformation can sometimes introduce space leaks. In your case it wouldn't be dangerous, but you cannot rely on your compiler recognizing this.
Florian Kreidler wrote: >> f :: Int -> Int >> f x = fst (expensive x) + snd (expensive x)
>> Will 'expensive x' be evaluated twice to compute 'f x'?
> That will depend on your compiler. Some compilers do common > subexpression elimination, others don't. The reason is that > this transformation can sometimes introduce space leaks. In > your case it wouldn't be dangerous, but you cannot rely on > your compiler recognizing this.