[Haskell-cafe] stuck with a sample of "programming in haskell"

国平张 zhangguoping at gmail.com
Wed Mar 17 00:09:43 EDT 2010


I am a beginner for haskell. I was stuck with a sample of "programming
in haskell". Following is my code:
import Prelude hiding (return, fail)

type Parser a = (String->[(a,String)])

return :: a -> Parser a
return v = (\inp->[(v,inp)])

item :: Parser Char
item = \inp -> case inp of
                   [] -> []
                   (x:xs) -> [(x,xs)]
failure :: Parser a
failure = \inp -> []

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

But it cannot be loadded by Hug, saying:

Couldn't match expected type `Char'
      against inferred type `[(Char, String)]'
 Expected type: [((Char, Char), String)]
 Inferred type: [(([(Char, String)], [(Char, String)]), String)]
In the expression: return (x, y)
In the expression:
   do x <- item
      y <- item
      return (x, y)


I googled and tried a few days still cannot get it compiled, can
someone do me a favor to point out what's wrong with it :-) ?

More information about the Haskell-Cafe mailing list