[Haskell-cafe] Golfing parsec code

Stephen Tetley stephen.tetley at gmail.com
Sat Oct 16 16:59:42 EDT 2010


The avoiding /try/ is a good part of Parsec golf. Because turning
natural literals into fractions is easy (%1) it is simple to use the
/option/ parser to parse a suffix or return a default.

/symbol/ is also a valuable parser, often preferable to /char/ or
/string/ as it chomps trailing white space.


symbol     = P.symbol lexer


fraction :: Parser Rational
fraction = do
    num <- integer
    den <- option 1 (symbol "/" >> natural)
    return (num % den)


More information about the Haskell-Cafe mailing list