[Haskell-cafe] [Newbie question] -- Looping stdin until condition is met

Martin Blais blais at furius.ca
Fri May 30 20:28:37 EDT 2008

Allright, this is a definitely a newbie question.

I'm learning Haskell and going through the exercises in the
beautiful Hutton book, and one of them requires for me to
write a loop that queries a line from the user (stdin),
looping until the user enters a valid integer (at least
that's how I want to implement the interface to the
exercise). I have tried tons of variants and modifications
of code, and I can't find the way to implement this. Here is
what my emacs buffer is at now::

  import Text.Read
  import System.IO
  import qualified Control.Exception as C

  getNum :: IO Int
  getNum = do line <- (hGetLine stdin)
              x <- (C.catch (do return (read line :: Int)) (\e -> getNum))
              return x

  main = do x <- getNum
            putStr ((show x) ++ "\n")

Now, I've tried the Prelude's catch, the Control.Exception
catch, I've tried moving it at the top of getnum, I've tried
without catch, I've tried a ton of other shtuff, so much
that I'm starting to think that Emacs is going to run out of
electrons soon. I asked some half-newbie friends who are
insanely enthousiastic about Haskell and they can't do it
either (I'm starting to think that those enthousiastic
friends are dating a beautiful girl with 3 PhDs, but she has
a 2-inch thick green and gooey wart smack on her nose and
they're so blindly in love that they can't admit that she
does). I've asked some university profs and they sidetrack
my question by saying I shouldn't do I/O so early. Can
anyone here restore my faith in the Haskell section of

1. How do I catch the exception that is raised from "read"?

2. Where do I find the appropriate information I need in
   order to fix this? I'm probably just not searching in the
   right place. (Yes, I've seen the GHC docs, and it doesn't
   help, maybe I'm missing some background info.)

3. Please do not tell me I should solve the problem
   differently. Here is the problem I'm trying to solve, and
   nothing else:

     "Write a program that reads a line from the user,
     looping the query until the line contains a valid

It shouldn't be too hard i think. The best answer would be a
two-liner code example that'll make me feel even more stupid
than I already do.

Thanks in advance.

More information about the Haskell-Cafe mailing list