I'm given the following code
newtype Parser a = Parser { parse :: String -> Maybe (a,String) }
instance Applicative Parser where
pure a = Parser $ \s -> Just (a,s)
f <*> a = Parser $ \s ->
case parse f s of
Just (g,s') -> parse (fmap g a) s'
Nothing -> Nothing
instance Alternative Parser where
empty = Parser $ \s -> Nothing
l <|> r = Parser $ \s -> parse l s <|> parse r s
satisfy :: (Char -> Bool) -> Parser Char
satisfy p = Parser f
where f [] = Nothing
f (x:xs) = if p x then Just (x,xs) else Nothing
ws :: Parser ()
ws = pure () <* many (satisfy isSpace)
I know that ws parser removes leading spaces. But I need an explanation how exactly this is done. I don't understand the <*
syntax ( I know <*>
). Can some help me understand the syntax and why it is working ?