
I'm new to Haskell and have a stupid question. How can a catch a parsing error on a read? Something like let s = "e" in read s :: Int I've been searching for a trick and couldn't find one. I want to enter commands directly from the keyboard and don't want the program to abort if I do a typing error. Regards, Jacques

jbergeron:
I'm new to Haskell and have a stupid question.
How can a catch a parsing error on a read? Something like
let s = "e" in read s :: Int
I've been searching for a trick and couldn't find one. I want to enter commands directly from the keyboard and don't want the program to abort if I do a typing error.
maybeRead :: Read a => String -> Maybe a maybeRead s = case reads s of [(x, rest)] | all isSpace rest -> Just x _ -> Nothing There's an open ticket to add this to the base library.

Don Stewart
jbergeron:
I'm new to Haskell and have a stupid question.
How can a catch a parsing error on a read? Something like
let s = "e" in read s :: Int
I've been searching for a trick and couldn't find one. I want to enter commands directly from the keyboard and don't want the program to abort if
I
do a typing error.
maybeRead :: Read a => String -> Maybe a maybeRead s = case reads s of [(x, rest)] | all isSpace rest -> Just x _ -> Nothing
There's an open ticket to add this to the base library.
So reads (or readsPrec) is the "parser" equivalent to read. Should have catched that, sorry. Thanks for your time. Jacques
participants (2)
-
Don Stewart
-
Jacques Bergeron