Created
September 9, 2016 14:22
-
-
Save samidarko/ef999524283a224c258412218bc490a0 to your computer and use it in GitHub Desktop.
Returns True if the parentheses in the input string is balanced
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
parentheseBalance :: String -> Bool | |
parentheseBalance s = f s 0 | |
where | |
f s c | |
| s == [] = c == 0 -- if string is empty, return true if count == 0 | |
| c < 0 = False | |
| head s == '(' = f (tail s) (c+1) | |
| head s == ')' = f (tail s) (c-1) | |
| otherwise = f (tail s) c | |
-- parentheseBalance ")" == False | |
-- parentheseBalance "()(" == False | |
-- parentheseBalance "()" == True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment