[Haskell-cafe] Parsec Expected Type

Ryan Ingram ryani.spam at gmail.com
Fri Mar 28 21:12:25 EDT 2008


On 3/28/08, Paul Keir <pkeir at dcs.gla.ac.uk> wrote:
> What I'd like is to parse either the string "parameter", or the string ":".
> I'm using 'reserved' and 'symbol' because they seem to correspond well to
> the concepts in the language I'm parsing. I could try,
>
> tester3 = reserved "parameter" <|> do { symbol ":"; return () }

Actually this is exactly on the right track.  But I agree, it looks a
bit contrived.  Maybe this looks better to you?

> tester3 = reserved "parameter" <|> (symbol ":" >> return ())

Or you could factor this behavior out into a new combinator:

> or_ :: Parser a -> Parser b -> Parser ()
> or_ x y = (x >> return ()) <|> (y >> return ())

> tester3 = reserved "parameter" `or_` symbol ":"

  -- ryan


More information about the Haskell-Cafe mailing list