<div dir="ltr"><div style>-- So why does this code run out of memory?</div><div style><br></div><div>import Control.DeepSeq</div><div>import System.IO</div><div>import qualified Data.ByteString.Char8 as BS</div><div><br></div>
<div>scanl' :: NFData a => (a -> b -> a) -> a -> [b] -> [a]</div><div>scanl' f q ls = q : (case ls of</div><div> [] -> []</div><div> x:xs -> let q' = f q x</div>
<div> in q' `deepseq` scanl' f q' xs)</div><div><br></div><div><br></div><div>main = do</div><div> file <- openBinaryFile "/dev/zero" ReadMode</div><div> chars <- BS.hGetContents file</div>
<div> let rv = drop 100000000000 $ scanl' (+) 0 $ map fromEnum $ BS.unpack chars</div><div> print (head rv)</div><div><br></div><div>-- my scanl' implementation seems to do the right thing, because </div><div>
<br></div><div>main = print $ last $ scanl' (+) (0::Int) [0..]<br></div><div><br></div><div style>-- runs without blowing up. so am i creating a some thunk here? or is hGetContents storing values? any way to get the exception handler to print a trace of what caused the allocation?</div>
<div><br></div></div>