title: Haskell Compiled IO-Action order and Flushing
body: <p>I'm encountering strange behaviour with IO, within compiled Haskell code. Here's what's going on:</p>
  
  <pre><code>-- MyScript.hs
  main = do
      putStr "Enter your name: "
      a &lt;- getLine
      putStrLn (a ++ " - that's a nice name!")
  </code></pre>
  
  <p>I run this in GHCi by calling <code>main</code> and it works as one would expect, first printing <code>Enter your name:</code> and then doing whatever it's to do afterwards. However, when I compile it with GHC (With and without <code>--make</code>), it first prompts for a line, and <em>then</em> prints everything at once, like this:</p>
  
  <pre><code>$ ./MyScript
  Jimmy Johnson
  Enter your name: Jimmy Johnson - That's a nice name!
  </code></pre>
  
  <p>To clarify, I want it to occur in the following sequence:</p>
  
  <pre><code>$ ./MyFixedScript
  Enter your name: Jimmy Johnson
  Jimmy Johnson - That's a nice name!
  </code></pre>
  
  <p>Could someone explain why this happens as it is, and how to sequence the IO the way that I would expect it to.</p>
  
  <p>Note also that I've tried changing the first line of the <code>do</code> statement to <code>_ &lt;- putStr "Enter your name: "</code>, but that still doesn't work.</p>