Sha256: 9e6d526443f08b38f88ccdaf6cda09d1cdc46d7a5b8a41c9fe199810cb1f784a
Contents?: true
Size: 928 Bytes
Versions: 254
Compression:
Stored size: 928 Bytes
Contents
module Brackets (arePaired) where data BracketType = Opening | Closing data Stack a = Empty | Elem a (Stack a) push :: Char -> Stack Char -> Stack Char push = Elem pop :: Stack Char -> Stack Char pop Empty = Empty pop (Elem _ stack) = stack arePaired :: String -> Bool arePaired xs = checkBalance xs Empty checkBalance :: String -> Stack Char -> Bool checkBalance [] Empty = True checkBalance [] _ = False checkBalance (x:xs) stack = case classify x of Just Opening -> checkBalance xs $ push x stack Just Closing -> (x `closes` stack) && checkBalance xs (pop stack) _ -> checkBalance xs stack classify :: Char -> Maybe BracketType classify x | x `elem` "([{" = Just Opening | x `elem` ")]}" = Just Closing | otherwise = Nothing closes :: Char -> Stack Char -> Bool closes _ Empty = False closes x (Elem y _) = x == ')' && y == '(' || x == ']' && y == '[' || x == '}' && y == '{'
Version data entries
254 entries across 254 versions & 1 rubygems