Will GHC transform an expression with intermediate values as efficiently as one without?
e.g.
main = print $ f ["aa", "bb", "cc"]
f x =
let a = map (map toUpper) x
b = filter (\z -> 'C' /= head z) a
c = foldl1 (++) b
in c
seems to have very different core output (with -ddump-simple) than with
f x = foldl1 (++) $ filter (\z -> 'C' /= head z) $ map (map toUpper) x
Could an expression with intermediate values take (significantly) longer to evaluate?
Linear use of intermediate
letbindings is equivalent to putting(.)between the values.GHC will fuse through such pipelines. You can see from the results of
-ddump-simpl-statsWith let Bindings:
Using a pipeline:
And the same fused worker:
With let Bindings:
Pipeline:
Did you forget to compile with -O2 ?