title: Why is the tail call optimization not used in this Haskell program?
body: <p>The following program blows the stack:</p>
  
  <pre><code>__find_first_occurrence :: (Eq b) =&gt; b -&gt; [b] -&gt; Int -&gt; Int
  __find_first_occurrence e [] i = -1
  __find_first_occurrence e (x:xs) i
      | e == x = i
      | otherwise = __find_first_occurrence e xs (i + 1)
  
  find_first_occurrence :: (Eq a) =&gt; a -&gt; [a] -&gt; Int
  find_first_occurrence elem list =   
      __find_first_occurrence elem list 0
  
  main = do
      let n = 1000000
      let idx = find_first_occurrence n [1..n]
      putStrLn (show idx)
  </code></pre>
  
  <p>Fails with</p>
  
  <blockquote>
    <p>Stack space overflow: current size 8388608 bytes. Use `+RTS -Ksize
    -RTS' to increase it.</p>
  </blockquote>
  
  <p>However, as far as I understand it, the possible recursive call to <code>__find_first_occurrence</code> is the last thing evaluated by <code>__find_first_occurrence</code>, hence tail call optimization should be possible to do.</p>