[Haskell-beginners] Help with monad syntax?

Arindam Roy arind_roy at fast-mail.org
Mon Aug 25 08:12:25 EDT 2008

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
understand the error I'm seeing when I try to load the program into

Following the book, I've defined a Parser type, then written the basic
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).

I've given below the entire file I've created and the error message I
get on
trying to load it. Any help much appreciated - Arindam

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
             y <- item
             Main.return (x, y)


Hugs> :r
ERROR file:c:\parsers.hs:23 - Type error in explicitly typed binding
*** Term           : p
*** Type           : [Char] ->
*** Does not match : Parser (Char,Char)

  Arindam Roy
  arind_roy at fast-mail.org

http://www.fastmail.fm - IMAP accessible web-mail

More information about the Beginners mailing list