Combining parsers in Haskell
问题 I'm given the following parsers newtype Parser a = Parser { parse :: String -> Maybe (a,String) } instance Functor Parser where fmap f p = Parser $ \s -> (\(a,c) -> (f a, c)) <$> parse p s 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 ensure :: (a -> Bool) ->