solution.hs 731 B

123456789101112131415161718192021
  1. -- https://www.codewars.com/kata/5277c8a221e209d3f6000b56
  2. module Codewars.Kata.Braces where
  3. validBraces :: String -> Bool
  4. validBraces xs = impl 0 0 0 xs where
  5. impl cnt1 cnt2 cnt3 str =
  6. if str == [] then
  7. cnt1 == 0 && cnt2 == 0 && cnt3 == 0
  8. else let
  9. x = head str
  10. xs = tail str
  11. in
  12. if cnt1 < 0 || cnt2 < 0 || cnt3 < 0 then
  13. False
  14. else if x == '(' then impl (cnt1 + 1) cnt2 cnt3 xs
  15. else if x == ')' then impl (cnt1 - 1) cnt2 cnt3 xs
  16. else if x == '[' then impl cnt1 (cnt2 + 1) cnt3 xs
  17. else if x == ']' then impl cnt1 (cnt2 - 1) cnt3 xs
  18. else if x == '{' then impl cnt1 cnt2 (cnt3 + 1) xs
  19. else if x == '}' then impl cnt1 cnt2 (cnt3 - 1) xs
  20. else False