[Haskell-cafe] Monadic parser vs. combinator parser

Ertugrul Söylemez es at ertes.de
Wed Jan 30 13:38:47 CET 2013

Jan Stolarek <jan.stolarek at p.lodz.pl> wrote:

> I will be writing a parser in Haskell and I wonder how to approach the
> problem. My first thought was to use monadic parser, e.g. like the one
> described by Hutton and Meijer in "Monadic Parsing in Haskell"
> functional pearl. But then I stumbled upon this:
> https://github.com/alephnullplex/cradle/tree/master/code/src/Lbach/Parser
> Monadic parser seems extremely verbose and not very straightforward
> compared to this one. I started to wonder whether I should use monadic
> parser for the sake of it being monadic or should I just go with the
> combinator approach? Any thoughts will be appreciated before I shoot
> myself in the foot :)

A monadic parser /is/ a combinator parser.  The code you linked just
doesn't go as far as wrapping it up with a newtype and providing a monad

Monadic parsers aren't verbose, because there is the applicative style.
Let's rewrite this noisy example (assuming automatic backtracking):

    inParens c = do
        char '('
        x <- c
        char ')'
        return x

All monads are also applicative functors, which means that you can use
applicative style to write this one more nicely:

    inParens c = char '(' *> c <* char ')'

If your parser is also an IsString you could even write:

    inParens c = "(" *> c <* ")"

If that's not nice and concise I don't know what is. =)


Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130130/d7c98145/attachment.pgp>

More information about the Haskell-Cafe mailing list