| |
comp.lang.haskell |
Frank Poettgen <Frank.Poett...@Post.RxWyTxH-AyAzCyHzEyN.de> schrieb: > expensive :: Int -> (Int,Int) > f :: Int -> Int > Will 'expensive x' be evaluated twice to compute 'f x'? f x = fst e + snd e where e = expensive or, using pattern matching, f x = l + r where (l, r) = expensive x or, more compact but less comprehensible, f = uncurry (+) . expensive
> time. Hence I want to avoid unnecessary evaluations of 'expensive'.
> expensive x = (x,x)
> f x = fst (expensive x) + snd (expensive x)
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.