Guest User

Untitled

a guest
May 25th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. type Parser a = String -> [(a, String)]
  2.  
  3. result :: a -> Parser a
  4. result v = \inp -> [(v, inp)]
  5.  
  6. zero :: a -> Parser a
  7. zero v = \inp -> []
  8.  
  9. item :: Parser Char
  10. item =
  11. \inp ->
  12. case inp of
  13. [] -> []
  14. (x:xs) -> [(x, xs)]
  15.  
  16. bind :: Parser a -> (a -> Parser b) -> Parser b
  17. p `bind` f = \inp -> concat [f v inp' | (v, inp') <- p inp]
  18.  
  19. seq :: Parser a -> Parser b -> Parser (a, b)
  20. p `seq` q = p `bind` \x -> q `bind` \y -> result (x, y)
  21.  
  22. sat :: (Char -> Bool) -> Parser Char
  23. sat p =
  24. item `bind` \x ->
  25. if p x
  26. then result x
  27. else zero x
  28.  
  29. lower :: Parser Char
  30. lower = sat (\x -> 'a' <= x && x <= 'z')
Add Comment
Please, Sign In to add comment