[Haskell-cafe] Parsec question

Thomas Conway drtomc at gmail.com
Thu Jun 21 06:36:56 EDT 2007


On 6/21/07, Dave Tapley <dukedave at gmail.com> wrote:
> > primary = (identifier >>= (return . PrimaryIdentifier)) <|> (stringLiteral >>= (return . PrimaryLiteral))
> > identifier = (many1 letter) >>= (return . Identifier)
> > stringLiteral = (char '\'') >> (manyTill anyChar (char '\'')) >>= (return . StringLiteral)

I have found this a sufficiently common pattern that I have a little
combinator to tidy it up:

p `with` f = p >>= (return . f)

so I can write

primary = (identifier `with` PrimaryIdentifier) <|> (stringLiteral
`with` PrimaryLiteral)

Obviously you could write it in terms of liftM, choose a different name, &c, &c.

FWIW, the other little combinator I invariably use is

p `returning` x = p >>= (\_ -> return x)

which I end up calling with () as the second argument so often
(especially during development), I usually have another combinator

void p = p >> return ()

YMMV,
T.
-- 
Dr Thomas Conway
drtomc at gmail.com

Silence is the perfectest herald of joy:
I were but little happy, if I could say how much.


More information about the Haskell-Cafe mailing list