title: Haskell foldl' poor performance with (++)
body: <p>I have this code:</p>
  
  <pre><code>import Data.List
  
  newList_bad  lst = foldl' (\acc x -&gt; acc ++ [x*2]) [] lst
  newList_good lst = foldl' (\acc x -&gt; x*2 : acc) [] lst
  </code></pre>
  
  <p>These functions return lists with each element multiplied by 2:</p>
  
  <pre><code>*Main&gt; newList_bad [1..10]
  [2,4,6,8,10,12,14,16,18,20]
  *Main&gt; newList_good [1..10]
  [20,18,16,14,12,10,8,6,4,2]
  </code></pre>
  
  <p>In ghci:</p>
  
  <pre><code>*Main&gt; sum $ newList_bad [1..15000]
  225015000
  (5.24 secs, 4767099960 bytes)
  *Main&gt; sum $ newList_good [1..15000]
  225015000
  (0.03 secs, 3190716 bytes)
  </code></pre>
  
  <p>Why <code>newList_bad</code> function works 200 times slower than <code>newList_good</code>? I understand that it's not a good solution for that task. But why this innocent code works so slow?</p>
  
  <p>What is this "4767099960 bytes"?? For that simple an operation Haskell used 4 GiB??</p>
  
  <p>After compilation:</p>
  
  <pre><code>C:\1&gt;ghc -O --make test.hs
  C:\1&gt;test.exe
  225015000
  Time for sum (newList_bad [1..15000]) is 4.445889s
  225015000
  Time for sum (newList_good [1..15000]) is 0.0025005s
  </code></pre>