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

Stephen Tetley stephen.tetley at gmail.com
Fri Mar 19 06:33:52 EDT 2010


Hi

I'm sorry about that, I should have check the last message runs, but I
typed it from a computer that I don't develop on. The code below
should run as I've tested it this time.



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 = P (\inp -> case inp of
                    [] -> []
                    (x:xs) -> [(x,xs)])

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

----------------------------------------

For the record - the error in the last code I sent was that the
newtype Parser has a different constructor name /P/ to its type name
/Parser/ - I hadn't spotted that in the untested code.

Apologies again

Stephen


More information about the Haskell-Cafe mailing list