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

Version Path
trackler-2.2.1.180 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.179 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.178 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.177 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.176 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.175 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.174 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.173 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.172 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.171 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.170 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.169 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.167 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.166 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.165 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.164 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.163 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.162 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.161 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs
trackler-2.2.1.160 tracks/haskell/exercises/bracket-push/examples/success-standard/src/Brackets.hs