title: How to properly use the readMaybe function in IO
body: <p>I started with programming in Haskell about 4 month ago and now I came to the point where I have to deal with the IO system of Haskell. 
  I already did a lot of IO actions and haven't faced any problems I couldn't solve by myself, but this time I googled for almost two hours for no avail to get some information about the function readMaybe. So I have the following problem set to solve and I already tried a lot of different approaches to solve it but all the time I get the same failure message from my compiler:</p>
  
  <pre><code>No instance for (Read a0) arising from a use of `readMaybe'
  The type variable `a0' is ambiguous
  </code></pre>
  
  <p>I understand what the compiler does want to tell me but I have no idea how to solve this problem. I already tried to add a class constraint, but without success. 
  So here is my very small and simple program that is just counting how many valid numbers the user has entered. The program is meant to terminate when the user enters an empty line.
  This is just a auxiliary function I want to use for my project later on.</p>
  
  <pre><code>countNumbers :: IO Int
  countNumbers = do
         x &lt;- count 0
         return x where
            count :: Int -&gt; IO Int
            count n = do
                line &lt;- getLine
                case line of
                   "" -&gt; do
                      return n
                   _  -&gt; case readMaybe line of
                      Just _ -&gt; do
                         x &lt;- count (n+1)
                         return x
                      Nothing -&gt; do
                         x &lt;- count n
                         return x
  </code></pre>
  
  <p>Unfortunately I couldn't find out a lot of informations about the function readMaybe. The only thing I could find was in the Haskell library Text.Read:</p>
  
  <pre><code>readMaybe :: Read a =&gt; String -&gt; Maybe aSource
  Parse a string using the Read instance. Succeeds if there is exactly one valid result.
  </code></pre>
  
  <p>The very weird thing for me is that I have already written such a function that uses the readMaybe function and it worked perfectly ...
  This program is just asking the user for a number and keeps asking as long as the user enters a valid number</p>
  
  <pre><code>getLineInt :: IO Int
  getLineInt = do
        putStrLn "Please enter your guess"
        line &lt;- getLine
        case readMaybe line of
              Just x -&gt; do
                 return x
              Nothing -&gt; do
                 putStrLn "Invalid number entered"
                 x &lt;- getLineInt
                 return x
  </code></pre>
  
  <p>So far as I can see there are no differences between the usage of the function readMaybe in the two programs and therefore it works in the one but not in the other :)</p>
  
  <p>I would be really thankful for any hints from you!!</p>