Where is the space leak in this code?
<p>I am trying to split a file into two separate files by alternating lines. (i.e. lines 1,3,5,7.. written to file 1 and lines 2,4,6,8... written to file 2).</p>

<p>The file I am working with is ~700MB, so when I seen the memory usage balloon over 6GB, I know something is wrong.</p>

<pre><code>main :: IO()
main = withFile splitFile ReadMode splitData
  where
    splitData h = do
      dataSet &lt;- lines &lt;$&gt; hGetContents h
      let (s1,s2) = foldl' (\(l,r) x -&gt; (x:r,l)) ([],[]) dataSet
      writeFile testFile $ unlines s1
      writeFile trainingFile $ unlines s2
</code></pre>

<p>I initially was using the lazy version of foldl, but after some research it seemed that using the strict version would help.  But alas, it made no noticeable difference. I also tried compiling with -O2, but that did nothing either. </p>

<p>I am using GHC 7.10.2</p>

<p>I'm not getting a stack overflow, so what is it using all that memory for?</p>