<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&#39; :: NFData a =&gt; (a -&gt; b -&gt; a) -&gt; a -&gt; [b] -&gt; [a]</div><div>scanl&#39; f q ls =  q : (case ls of</div><div>                        []   -&gt; []</div><div>                        x:xs -&gt; let q&#39; = f q x</div>
<div>                                in q&#39; `deepseq` scanl&#39; f q&#39; xs)</div><div><br></div><div><br></div><div>main = do</div><div>   file &lt;- openBinaryFile &quot;/dev/zero&quot; ReadMode</div><div>   chars &lt;- BS.hGetContents file</div>
<div>   let rv = drop 100000000000 $ scanl&#39; (+) 0 $ map fromEnum $ BS.unpack chars</div><div>   print (head rv)</div><div><br></div><div>-- my scanl&#39; implementation seems to do the right thing, because </div><div>
<br></div><div>main = print $ last $ scanl&#39; (+) (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>