Created
October 13, 2016 15:40
-
-
Save ethagnawl/593cc35d53c4a2819607403cfc3888e1 to your computer and use it in GitHub Desktop.
applicative parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Applicative | |
import Data.Char | |
newtype Parser a = P (String -> [(a, String)]) | |
instance Functor Parser where | |
fmap g p = P (\inp -> case parse p inp of | |
[] -> [] | |
[(v, out)] -> [(g v, out)]) | |
instance Applicative Parser where | |
pure v = P (\inp -> [(v, inp)]) | |
pg <*> px = P (\inp -> case parse pg inp of | |
[] -> [] | |
[(g, out)] -> parse (fmap g px) out) | |
parse (P p) inp = p inp | |
item = P (\inp -> case inp of | |
[] -> [] | |
(x:xs) -> [(x, xs)]) | |
three = pure g <*> item <*> item <*> item | |
where g x y z = (x, z) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment