Created
November 19, 2015 08:59
-
-
Save groz/ca57346e39c690ec6be1 to your computer and use it in GitHub Desktop.
fp101x parsers lecture working example
This file contains hidden or 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
module Parsers where | |
import Prelude hiding (fail, return, (>>=)) | |
type Parser a = String -> [(a, String)] | |
item :: Parser Char | |
item [] = [] | |
item (c:cs) = [(c, cs)] | |
fail :: Parser a | |
fail _ = [] | |
return :: a -> Parser a | |
return v inp = [(v, inp)] | |
(>>=) :: Parser a -> (a -> Parser b) -> Parser b | |
p >>= f = \inp -> concat [f a out | (a, out) <- p inp] | |
p :: Parser (Char, Char) | |
p = item >>= \c1 -> | |
item >>= \_ -> | |
item >>= \c2 -> | |
return (c1, c2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment