[Haskell-cafe] newbie question on Parsers from "Programming In Haskell"

Alfonso Acosta alfonso.acosta at gmail.com
Mon Jun 4 10:48:52 EDT 2007


On 6/4/07, Juozas Gaigalas <juozas at tuesdaystudios.com> 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

Although the error is not really friendly (your "supposedly" last
generator in do is an expression) the problem probably is indentation:

Try like this

>  p = do x <- item
>             item
>             y <- item
>             return (x, y)  -- LINE 34

Hugs is probably complaining because it identifies "x <- item" (which
is not a simple expression) as the last element of do.


More information about the Haskell-Cafe mailing list