I have been reading a tutorial about parser combinators and I came across a function which I would like a bit of help in trying to understand.
satisfy :: (Char -> Bool) -> Parser Char
satisfy p = item `bind` \c ->
if p c
then unit c
else (Parser (\cs -> []))
char :: Char -> Parser Char
char c = satisfy (c ==)
natural :: Parser Integer
natural = read <$> some (satisfy isDigit)
string :: String -> Parser String
string [] = return []
string (c:cs) = do { char c; string cs; return (c:cs)}
My question is how does the string function work or rather how does it terminate, say i did something like:
let while_parser = string "while"
and then i used it to parse a string say for example
parse while_parser "while if" , it will correctly parse me the "while".
however if i try something like parse while_parser "test it will return [].
My question is how does it fail? what happens when char c returns an empty list?
char cdoesn't "return an empty list", rather it fails on end of input. The bind operator then propagates that failure.let while_parser = string "while", right?