title: Read single line from stdin in Haskell using IO.readLn
body: <p>This code does not compile in GHC 7.0.3:</p>
  
  <pre><code>import System.IO
  
  main = do
      z &lt;- readLn
      print z
  </code></pre>
  
  <p>My intention is to read one line from stdin and store it in z, to do more advanced stuff with it later on. Error message looks like:</p>
  
  <pre><code>test.hs:5:9:
      Ambiguous type variable `a0' in the constraints:
        (Show a0) arising from a use of `print' at test.hs:5:9-13
        (Read a0) arising from a use of `readLn' at test.hs:4:14-19
      Probable fix: add a type signature that fixes these type variable(s)
      In a stmt of a 'do' expression: print z
      In the expression:
        do { z &lt;- readLn;
             print z;
             return () }
      In an equation for `main':
          main
            = do { z &lt;- readLn;
                   print z;
                   return () }
  </code></pre>
  
  <p>Obviously there is something fundamental I haven't understood yet; please explain to me why it doesn't work and how to fix it.</p>
  
  <p><strong>EDIT1</strong>: I fixed the compile error by changing <code>print z</code> to <code>putStrLn z</code>, so GHC understands that I want to read a string. But when I run the program, I get a runtime error which I can't understand:</p>
  
  <pre><code>$ ./test
  hello!
  test: user error (Prelude.readIO: no parse)
  $
  </code></pre>
  
  <p>I just typed "hello!" and then enter. Note that I'm running x86_64 GHC on OS X, which is considered unstable.</p>
  
  <p><strong>EDIT2</strong>: I changed readLn to getLine and it magically works for no reason. I would like to know why, but I'm happy it works.</p>
  
  <p>Final code:</p>
  
  <pre><code>import System.IO
  
  main = do
      z &lt;- getLine
      print z
  </code></pre>