Skip to content

Commit

Permalink
Add parsers code
Browse files Browse the repository at this point in the history
  • Loading branch information
mroman42 committed Jun 24, 2017
1 parent 5125cdd commit 7b47c98
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions parsers.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Control.Monad

newtype Parser a = Parser (String -> [(a,String)])

parse :: Parser a -> String -> [(a,String)]
parse (Parser p) = p

instance Functor Parser where
fmap f p = Parser (\s -> [(f x, s') | (x,s') <- parse p s])

instance Applicative Parser where
pure = return
(<*>) = ap

instance Monad Parser where
return x = Parser (\s -> [(x,s)])
p >>= q = Parser (\s -> concat [parse (q x) s' | (x,s') <- parse p s ])

item :: Parser Char
item = Parser (\s -> case s of
"" -> []
(c:s') -> [(c,s')])

many :: Parser a -> Parser [a]
many p = do
a <- p
as <- many p
return (a:as)

main = return ()

0 comments on commit 7b47c98

Please sign in to comment.