- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
import Data.List
import Data.Maybe
checkBraces "" = True
checkBraces e = checkBrace e [] where
openBraces = "({["
closeBraces = ")}]"
braceToBrace fromBraces toBraces brace = toBraces!!(fromJust $ elemIndex brace fromBraces)
otcBrace = braceToBrace openBraces closeBraces
checkBrace (c:e) s | elem c openBraces = checkBrace e ((otcBrace c):s)
checkBrace (c:e) (h:s) | (elem c closeBraces) && (h==c) = checkBrace e s
checkBrace (_:e) _ = False
checkBrace [] [] = True
checkBrace [] _ = False
main = mapM_ (print . checkBraces) bracesExpressionExamples where
bracesExpressionExamples =
[
"({[{}]{}[]})",
"({}}{[{}]{}[]})",
"({[{}]{}[]}",
"({[{}]{}]})",
"({[{}{}[]})",
"",
"{}"
]