This simple program runs in constant memory space when compiled with no flags with ghc:
import Data.List
f x = x*x
g a = foldl' (+) (f a) [1..(1073741824-1)]
main = do putStrLn $ show $ foldl' (+) 0 $ map g [0,1]
When compiled with ghc -O2 the memory usage exceeds the system resources (8GB).
Changing main to:
main = do putStrLn $ show $ foldl' (+) 0 [g 0, g 1]
alleviates the problem so it appears to be something to do with the map.
Can anyone explain the behaviour and perhaps how to work around it?
GHC version is: Glasgow Haskell Compiler, Version 7.4.1, stage 2 booted by GHC version 7.4.1