Last active
June 18, 2017 02:47
-
-
Save TGOlson/b991c841342f765c85015e2d03b8570c 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
hasMatchingBrackets :: String -> Bool | |
hasMatchingBrackets = fst . foldr reducingFn (True, mempty) | |
where | |
reducingFn _ (False, xs) = (False, xs) -- short circuit if already false | |
reducingFn char (True, tokens) | char `elem` openingTokens = (True, char : tokens) | |
reducingFn char (True, []) | char `elem` closingTokens = (False, []) | |
reducingFn char (True, x:xs) | char `elem` closingTokens = if isClosingToken x char then (True, xs) else (False, x:xs) | |
reducingFn _ st = st | |
tokenPairs :: [(Char, Char)] | |
tokenPairs = [ ('(', ')'), ('{', '}')] | |
openingTokens :: String | |
openingTokens = fst <$> tokenPairs | |
closingTokens :: String | |
closingTokens = snd <$> tokenPairs | |
isClosingToken :: Char -> Char -> Bool | |
isClosingToken opening closing = undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment