[Haskell-beginners] Help with monad syntax?
arind_roy at fast-mail.org
Tue Aug 26 03:44:14 EDT 2008
> 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 = ...
Thanks, I'll try this. I haven't read upto the data declaration and inheritance
yet, but I'll try it as soon as I have :-)
> > (>>=) :: 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?
The way Parsers are designed in the book, it can't. Parsers always return a list
with one element. The only reason the return value is a list is to use the empty
list as an error indication.
More information about the Beginners