Skip to content

Instantly share code, notes, and snippets.

@ironhouzi
Created May 21, 2018 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ironhouzi/ecd2d8b731fb4870c5290e32c5653c93 to your computer and use it in GitHub Desktop.
Save ironhouzi/ecd2d8b731fb4870c5290e32c5653c93 to your computer and use it in GitHub Desktop.
type Parser a = String -> [(a, String)]
result :: a -> Parser a
result v = \inp -> [(v, inp)]
zero :: a -> Parser a
zero v = \inp -> []
item :: Parser Char
item =
\inp ->
case inp of
[] -> []
(x:xs) -> [(x, xs)]
bind :: Parser a -> (a -> Parser b) -> Parser b
p `bind` f = \inp -> concat [f v inp' | (v, inp') <- p inp]
seq :: Parser a -> Parser b -> Parser (a, b)
p `seq` q = p `bind` \x -> q `bind` \y -> result (x, y)
sat :: (Char -> Bool) -> Parser Char
sat p =
item `bind` \x ->
if p x
then result x
else zero x
lower :: Parser Char
lower = sat (\x -> 'a' <= x && x <= 'z')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment