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

Philip Weaver philip.weaver at gmail.com
Fri May 30 19:54:18 EDT 2008

On Fri, May 30, 2008 at 5:28 PM, Martin Blais <blais at furius.ca> wrote:
> 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
> humanity?
> 1. How do I catch the exception that is raised from "read"?
I think you want readIO, which yields a computation in the IO monad,
so it can be caught.

> 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.)
In this particular case, I am not sure where you'd find this
information.  It's not very intuitive to a beginner why "read" doesn't
work in this case.

> 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
>     integer."
> 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.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list