title: Why is the F# version of this program 6x faster than the Haskell one?
body: <p>Haskell version(1.03s):</p>
  
  <pre class="lang-hs prettyprint-override"><code>module Main where
    import qualified Data.Text as T
    import qualified Data.Text.IO as TIO
    import Control.Monad
    import Control.Applicative ((&lt;$&gt;))
    import Data.Vector.Unboxed (Vector,(!))
    import qualified Data.Vector.Unboxed as V
  
    solve :: Vector Int -&gt; Int
    solve ar =
      V.foldl' go 0 ar' where
        ar' = V.zip ar (V.postscanr' max 0 ar)
        go sr (p,m) = sr + m - p
  
    main = do
      t &lt;- fmap (read . T.unpack) TIO.getLine -- With Data.Text, the example finishes 15% faster.
      T.unlines . map (T.pack . show . solve . V.fromList . map (read . T.unpack) . T.words)
        &lt;$&gt; replicateM t (TIO.getLine &gt;&gt; TIO.getLine) &gt;&gt;= TIO.putStr
  </code></pre>
  
  <p>F# version(0.17s):</p>
  
  <pre class="lang-ml prettyprint-override"><code>open System
  
  let solve (ar : uint64[]) =
      let ar' = 
          let t = Array.scanBack max ar 0UL |&gt; fun x -&gt; Array.take (x.Length-1) x
          Array.zip ar t
  
      let go sr (p,m) = sr + m - p
      Array.fold go 0UL ar'
  
  let getIntLine() =
      Console.In.ReadLine().Split [|' '|]
      |&gt; Array.choose (fun x -&gt; if x &lt;&gt; "" then uint64 x |&gt; Some else None)    
  
  let getInt() = getIntLine().[0]
  
  let t = getInt()
  for i=1 to int t do
      getInt() |&gt; ignore
      let ar = getIntLine()
      printfn "%i" (solve ar)
  </code></pre>
  
  <p>The above two programs are the solutions for the <a href="https://www.hackerrank.com/challenges/stockmax/" rel="noreferrer">Stock Maximize problem</a> and times are for the first test case of the <code>Run Code</code> button.</p>
  
  <p>For some reason the F# version is roughly 6x faster, but I am pretty sure that if I replaced the slow library functions with imperative loops that I could speed it up by at least 3 times and more likely 10x.</p>
  
  <p>Could the Haskell version be similarly improved?</p>
  
  <p>I am doing the above for learning purposes and in general I am finding it difficult to figure out how to write efficient Haskell code.</p>