[Haskell-cafe] newbie question on Parsers from
"Programming In Haskell"
Ilya Tsindlekht
eilya497 at 013.net
Tue Jun 5 03:08:43 EDT 2007
On Mon, Jun 04, 2007 at 05:42:35PM +0300, Juozas Gaigalas wrote:
> Hello,
>
> I am a somewhat experienced programmer and a complete Haskell newbie, so I
> hope this is the correct ML for my question.
>
> I have decided to learn Haskell and started with Graham Hutton's book.
> Everything was going nicely until section 8.4, on sequencing functional
> parsers. I am trying to write an elementary parser that produces the 1st and
> 3d elements from a string. I am using the code from the book.
>
> ---------------------
>
> type Parser a = String -> [(a, String)]
>
> return :: a -> Parser a
> return v = \inp -> [(v, inp)]
>
> failure :: Parser a
> failure = \inp -> []
>
>
> item :: Parser Char
> item = \inp -> case inp of
> [] -> []
> (x:xs) -> [(x, xs)]
>
>
> parse :: Parser a -> String -> [(a, String)]
> parse p inp = p inp
>
>
> (>>=) :: Parser a -> (a -> Parser b) -> Parser b
> p >>= f = \inp -> case parse p inp of
> [] -> []
> [(v, out)] -> parse (f v) out
>
>
> p :: Parser (Char, Char)
> p = do x <- item
> item
> y <- item
> return (x, y) -- LINE 34
> --------------------
>
> BUT, when I try to :load parse.hs from hugs I get the following error:
>
> ERROR "parse.hs":34 - Last generator in do {...} must be an expression
>
>
> I have no idea what I am doing wrong and why Hugs is complaining. I hope
> this question is not too simply for this mailing list, but I have honestly
> googled for an answer and had found nothing.
>
>
If you wish to use 'do' notation with your parser, you must declare it
as an instance of Monad class.
>
> Juozas Gaigalas
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
More information about the Haskell-Cafe
mailing list