title: Interleaving list functions
body: <p>Lets say I'm given two functions:</p>
  
  <pre><code>f :: [a] -&gt; b
  g :: [a] -&gt; c
  </code></pre>
  
  <p>I want to write a function that is the equivalent of this:</p>
  
  <pre><code>h x = (f x, g x)
  </code></pre>
  
  <p>But when I do that, for large lists inevitably I run out of memory.</p>
  
  <p>A simple example is the following:</p>
  
  <pre><code>x = [1..100000000::Int] 
  main = print $ (sum x, product x)
  </code></pre>
  
  <p>I understand this is the case because the list <code>x</code> is being stored in memory without being garbage collected. It would be better instead of <code>f</code> and <code>g</code> worked on <code>x</code> in, well, "parallel". </p>
  
  <p>Assuming I can't change <code>f</code> and <code>g</code>, nor want to make a separate copy of <code>x</code> (assume <code>x</code> is expensive to produce) how can I write <code>h</code> without running into out of memory issues?</p>