title: Is my rewritten foldl function optimised?
body: <p>I just started Haskell 2 days ago so I'm not yet sure about how to optimise my code.</p>
  
  <p>As an exercise, I have rewritten <code>foldl</code> and <code>foldr</code> ( I will give <code>foldl</code> here but <code>foldr</code> is the same, replacing <code>last</code> with <code>head</code> and <code>init</code> with <code>tail</code>).</p>
  
  <p>The code is:</p>
  
  <pre><code>module Main where
  
      myFoldl :: ( a -&gt; ( b -&gt; a ) ) -&gt; a -&gt; ( [b] -&gt; a )
  
      myFoldl func = ( \x -&gt; (\theList
          -&gt; if (length theList == 0) then
              x
          else
              myFoldl func (func x (last theList) ) (init theList)
          ) )
  </code></pre>
  
  <p>My only concern is that I suspect Haskell can't apply tail call optimisation here because the recursive call is not made at the end of the function.</p>
  
  <p>How can I make this tail call optimised? Is Haskell's built-in implementation of <code>foldl</code> implemented differently to mine?</p>