Last active
August 19, 2018 04:09
-
-
Save glguy/7e8f8474630e3498842c72c20e5d152c to your computer and use it in GitHub Desktop.
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
module Demo where | |
check :: String -> Bool | |
check = go [] | |
where | |
go ('[':xs) (']':ys) = go xs ys | |
go ('{':xs) ('}':ys) = go xs ys | |
go ('(':xs) (')':ys) = go xs ys | |
go xs (y:ys) | |
| y `elem` "[{(" = go (y:xs) ys | |
| otherwise = y `notElem` "]})" && go xs ys | |
go xs [] = null xs |
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
module Demo where | |
import Text.Megaparsec | |
import Text.Megaparsec.Char | |
type Parser = Parsec (ErrorItem Char) String | |
demo :: String -> IO () | |
demo = parseTest parser | |
parser :: Parser () | |
parser = parserN *> eof | |
-- | Parse many non-bracket characters and bracketed subexpressions. | |
parserN :: Parser () | |
parserN = skipMany parser1 | |
-- | Parse a single non-bracket character or a whole bracketed subexpression. | |
parser1 :: Parser () | |
parser1 = aux '(' ')' <|> | |
aux '[' ']' <|> | |
aux '{' '}' <|> | |
() <$ noneOf ")]}" | |
where | |
aux x y = () <$ between (char x) (char y) parserN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment