lalala33rfs

Untitled

Nov 19th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. import Control.Applicative
  2.  
  3. newtype Parser a = Parser { apply :: String -> [(a, String)] }
  4.  
  5. instance Functor Parser where
  6. fmap g (Parser p) = Parser $ \xs -> (map (\(x,y) -> (g x,y)) (p xs))
  7.  
  8. instance Applicative Parser where
  9. pure x = Parser $ \s -> [(x, s)]
  10.  
  11. Parser pf <*> Parser px = Parser (\s -> [ (f x, sx) | (f, sf) <- pf $ s, (x, sx) <- px $ sf])
  12.  
  13.  
  14. instance Alternative Parser where
  15. empty = Parser $ \_ -> []
  16. (Parser u) <|> (Parser v) = Parser (\s -> u s ++ v s)
Advertisement
Add Comment
Please, Sign In to add comment