[Haskell-cafe] Parsec question

Jules Bean jules at jellybean.co.uk
Thu Jun 21 08:52:56 EDT 2007

Thomas Conway wrote:
> p `with` f = p >>= (return . f)
> so I can write
> primary = (identifier `with` PrimaryIdentifier) <|> (stringLiteral
> `with` PrimaryLiteral)

I would write

primary = PrimaryIdentifier `fmap` identifer
       <|> PrimaryLiteral    `fmap` stringLiteral

(I prefer fmap to liftM but they are the same for monads). To my mind 
this fits the general pattern of 'constructor comes before contents'. 
with is, of course, just fmap with the parameters reversed.

It's a question of taste if it's better to define a new name or use an 
existing one.

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

I see no convincing reason to prefer that to

p >> return x

(which is fewer characters and, to me, just as clear).

In fact I'll also use

do { p ; return x }

and which of the two I choose will depend on context. If this is part of 
a large 'choice' construct I prefer to have each branch using the same 
notation (all do, or all not do).


More information about the Haskell-Cafe mailing list