Hi everyone,<br>I've been playing with the parsers decribed in "Monadic Parser Combinators" (<a href="http://www.cs.nott.ac.uk/~gmh/bib.html#monparsing">http://www.cs.nott.ac.uk/~gmh/bib.html#monparsing</a>) and I've gotten stumped. I'm trying to get comfortable working monadically, so please excuse my ignorance. Here's the relevant portions of my code:
<br><br>data Parser a = Parser { parse :: (String -> [(a, String)]) }<br><br>instance Monad Parser where<br> return v = Parser (\inp -> [(v, inp)])<br> par@(Parser p) >>= f =<br> Parser (\inp -> concat [parse (f v) out | (v, out) <- p inp])
<br><br>instance MonadPlus Parser where<br> mzero = Parser (\inp -> [])<br> p `mplus` q = Parser (\inp -> (parse p inp ++ parse q inp))<br><br>item :: Parser Char<br>item = Parser (\inp -> case inp of<br> [] -> []
<br> (x:xs) -> [(x, xs)])<br><br>sat :: (Char -> Bool) -> Parser Char<br>sat p = Parser (\inp -> [ (v, out) | (v, out) <- parse item inp, p v])<br><br>lower :: Parser Char<br>lower = Parser (\inp -> parse (sat (\x -> 'a' <= x && x <= 'z')) inp)
<br><br>upper :: Parser Char<br>upper = Parser (\inp -> parse (sat (\x -> 'A' <= x && x <= 'Z')) inp)<br><br>letter :: Parser Char<br>letter = lower `mplus` upper<br><br>-- word parses everything as []
<br>word :: Parser String<br>word = mzero `mplus` do<br> x <- letter;<br> xs <- word;<br> return (x:xs)<br><br>As I noted in the code, no matter what inputs I give it,<br>> parse word "blah blah"<br>
always returns []. Any ideas where where my misunderstanding is?<br><br>David<br><br>