[Haskell-beginners] Help with monad syntax?
Tillmann Rendel
rendel at daimi.au.dk
Mon Aug 25 10:29:08 EDT 2008
Arindam Roy wrote:
> I'm trying to learn Haskell using Hutton's book and Hugs (Sep 06),
> and am trying to work the parsing examples (chapter 8, Functional
> parsers), but I can't understand the error I'm seeing when I try to
> load the program into Hugs.
>
> Following the book, I've defined a Parser type, then written the basic
> parsers, then a sequencing operator >>=, followed by the example p
> parser (bottom of page 77). However, trying to load this file in Hugs
> generates an error (caused by the definition of p).
To make do notation work, you have to
(1) use a data type for Parser (instead of a type synonym) and
(2) declare that data type an instance of the Monad typeclass.
Your code should look something like this:
data Parser = Parser (...)
instance Monad Parser where
return v = ...
Parser p >>= f = ...
...
> (>>=) :: Parser a -> (a -> Parser b) -> Parser b
> p >>= f = \inp -> case parse p inp of
> [] -> []
> [(v, out)] -> parse (f v) out
What happens if p returns more then one result?
Tillmann
More information about the Beginners
mailing list