[Haskell-cafe] [Newbie question] -- Looping stdin until condition
blais at furius.ca
Fri May 30 20:24:42 EDT 2008
On Fri, 30 May 2008 17:19:54 -0700, "Philip Weaver"
<philip.weaver at gmail.com> said:
> > Dear Philip, could you point your virtual finger towards a
> > reference/paper/book/any-bleeping-thing that would help this simple
> > beginner understand why it doesn't work in this case? I'm trying to
> > picture why a "read" function that terminates the program would be
> > useful anywhere. In fact, any library function other than something like
> > UNIX's "exit" or "kill" which terminates my program is not really
> > welcome in any of my computer programs, but then again, I haven't yet
> > been illuminated by the genie of pure functional languages. A reference
> > would be awesome.
> Sorry, I wouldn't know where to point you, other than stating the
> simple rule that you can't catch exceptions in pure code. Others may
> be able to enlighten you better.
> By the way, the example that Dons gave may look more verbose, but
> (when possible) it's a probably a better idea to capture failure in a
> Maybe than in IO. I gave you "readIO" because it fit in to the
> exception handling that you were trying to do, and because you said
> you didn't want anyone to tell you you were doing things wrong :).
Here is a private reply from another user, which is more explanatory,
the problem was that the read function wasn't getting called at a point
where it could have been caught (I'll have to look into that into more
All you need is a little strictness,
x <- (C.catch (return $! read line :: Int) (\e -> getNum))
works. Another option is using evaluate instead of return.
The problem is that (read line :: Int) is not evaluated until it is
that is when it's going to be printed, but then it's too late to catch
Some general remarks:
hGetLine stdin === getLine
do x <- action
is the same as
More information about the Haskell-Cafe