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

国平张 zhangguoping at gmail.com
Fri Mar 19 00:35:27 EDT 2010


Sorry to bother again. I just cannot figure out how it could compile.
I got compile errors.
Can someone point out what is right code to use a do notion to make a
Parser works.

Thanks in advance.
--------------------------------------------------------------------------------------------
newtype Parser a = P { parse :: (String -> [(a,String)]) }

instance Monad Parser where
   return v = P (\s -> [(v,s)])
   p >>= f = P (\s -> case parse p s of
                       []    -> []
                       [(v,str)] -> parse (f v) str)
   fail _ = P (\_ -> [])


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

p :: Parser (Char,Char)
p = do x <- item
       item
       y <- item
       return (x,y)
---------------------------------------------------------------------------------


More information about the Haskell-Cafe mailing list