[Haskell-cafe] Re: Parsers are monadic?

Eric devnull1999 at yahoo.com
Sat Jun 30 14:55:47 EDT 2007

Gregory Propf <gregorypropf <at> yahoo.com> writes:

> First post. I'm a newbie, been using Haskell for about a
> week and love it. Anyway, this is something I don't
> understand. Parsers are monadic. I can see this if the
> parser is reading from an input stream but if there's just a
> block of text can't you just have the parser call itself
> recursively feeding the unparsed text down the recursion and
> tacking nodes onto the tree as the recursions return,
> finally returning the whole tree to the top level
> caller. Seems this meets the criteria for pure
> functionality, same result every time with same args.
> myParser :: [Char] -> ParseTree

Looks as if others may be answering questions you didn't ask.  

It seems to me as if your definition of "monadic" is a little off.

You're right: parsers (for a well-behaved grammar) *are* purely
functional.  Same input always gives the same output.

And you're right in your understanding that calculations that aren't
purely functional are handled in Haskell by a monad, specifically the
IO monad.

However--there are lots of monads in Haskell other than the IO monad
and many of them are purely functional.  Take Maybe: using the Maybe
monad is just syntactic sugar for what you'd get explicitly writing
out a chain of "and if this fails return Nothing for the whole
calculation, but if it succeeds, then...".

"Monadic" just means a calculation using a mathematical structure
called a monad.  All impure calculations in Haskell are monadic, but
not all monadic calculations are impure.

Does this answer your question?


More information about the Haskell-Cafe mailing list