Skip to content

Instantly share code, notes, and snippets.

@ethagnawl
Created October 13, 2016 15:40
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 ethagnawl/593cc35d53c4a2819607403cfc3888e1 to your computer and use it in GitHub Desktop.
Save ethagnawl/593cc35d53c4a2819607403cfc3888e1 to your computer and use it in GitHub Desktop.
applicative parser
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