[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-cafe
Subject:    Re: [Haskell-cafe] handling read exceptions
From:       Sven Panne <Sven.Panne () aedion ! de>
Date:       2004-04-13 6:54:35
Message-ID: 407B8EAB.7060707 () aedion ! de
[Download RAW message or body]

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.ParserCombinators.Parsec.html

or Happy:

    http://haskell.org/happy/

Cheers,
    S.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic