handling read exceptions

I want to read strings that look like "2" or "hello" into values of type Integer or String. The problem is that read requires that strings be read as "\"hello\"". Is there a way either to convince read to not require wrapping quotation marks or, alternetively, to catch a read exception, and do something sane? -Alex- _________________________________________________________________ S. Alexander Jacobson mailto:me@alexjacobson.com tel:917-770-6565 http://alexjacobson.com

I mean catch the read exception without escaping all the way out to the IO monad. -Alex- On Mon, 12 Apr 2004, S. Alexander Jacobson wrote:
I want to read strings that look like "2" or "hello" into values of type Integer or String. The problem is that read requires that strings be read as "\"hello\"". Is there a way either to convince read to not require wrapping quotation marks or, alternetively, to catch a read exception, and do something sane?
-Alex-
_________________________________________________________________ S. Alexander Jacobson mailto:me@alexjacobson.com tel:917-770-6565 http://alexjacobson.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_________________________________________________________________ S. Alexander Jacobson mailto:me@alexjacobson.com tel:917-770-6565 http://alexjacobson.com

S. Alexander Jacobson wrote:
I want to read strings that look like "2" or "hello" into values of type Integer or String. The problem is that read requires that strings be read as "\"hello\"". Is there a way either to convince read to not require wrapping quotation marks or, alternetively, to catch a read exception, and do something sane?
"reads" is probably what you are looking for: Prelude> (reads :: ReadS Integer) "" [] Prelude> (reads :: ReadS Integer) "a" [] Prelude> (reads :: ReadS Integer) "2" [(2,"")] Prelude> (reads :: ReadS Integer) "123blah" [(123,"blah")] And reading a string the way you want is best done by "id". :-) Cheers, S.

My point is that I am reading in name/value pairs and once I know the name, I know the type of the value, but I don't want to have to pass that information programatically to the point in the code where I am doing the read. -Alex- On Mon, 12 Apr 2004, Sven Panne wrote:
S. Alexander Jacobson wrote:
I want to read strings that look like "2" or "hello" into values of type Integer or String. The problem is that read requires that strings be read as "\"hello\"". Is there a way either to convince read to not require wrapping quotation marks or, alternetively, to catch a read exception, and do something sane?
"reads" is probably what you are looking for:
Prelude> (reads :: ReadS Integer) "" [] Prelude> (reads :: ReadS Integer) "a" [] Prelude> (reads :: ReadS Integer) "2" [(2,"")] Prelude> (reads :: ReadS Integer) "123blah" [(123,"blah")]
And reading a string the way you want is best done by "id". :-)
Cheers, S.
_________________________________________________________________ S. Alexander Jacobson mailto:me@alexjacobson.com tel:917-770-6565 http://alexjacobson.com

S. Alexander Jacobson wrote:
My point is that I am reading in name/value pairs and once I know the name, I know the type of the value, but I don't want to have to pass that information programatically to the point in the code where I am doing the read.
OK, I see... I don't know the exact syntax you are using (e.g. how are the strings terminated?), but "reads" is still useful: readIS :: ReadS (Either Integer String) readIS s = take 1 $ [ (Left x, t) | (x, t) <- reads s ] ++ [ (Right x, t) | (x, t) <- lex s ] Then we have: Main> readIS "123blah" [(Left 123,"blah")] Main> readIS "blah123" [(Right "blah123","")] Main> readIS "" [(Right "","")] Main> readIS "foo bar" [(Right "foo"," bar")] If you have only simple parsing tasks and are not looking for extreme performance, the Read class is a good choice. Otherwise you should probably have a look at the Parsec package which comes with Hugs and GHC: http://www.haskell.org/ghc/docs/latest/html/libraries/parsec/Text.ParserComb... or Happy: http://haskell.org/happy/ Cheers, S.

On 13/04/2004, at 2:17 AM, S. Alexander Jacobson wrote:
I want to read strings that look like "2" or "hello" into values of type Integer or String.
The problem is that read requires that strings be read as "\"hello\"". Is there a way either to convince read to not require wrapping quotation marks or, alternetively, to catch a read exception, and do something sane?
Untested code (I'll leave you to come up with a better name than MyRead :) class MyRead a where myRead :: String -> a instance MyRead Integer where myRead = read instance MyRead String where myRead = id -- % Andre Pang : trust.in.love.to.save
participants (3)
-
Andre Pang
-
S. Alexander Jacobson
-
Sven Panne