Skip to content

Instantly share code, notes, and snippets.

@samidarko
Created September 9, 2016 14:22
Show Gist options
  • Save samidarko/ef999524283a224c258412218bc490a0 to your computer and use it in GitHub Desktop.
Save samidarko/ef999524283a224c258412218bc490a0 to your computer and use it in GitHub Desktop.
Returns True if the parentheses in the input string is balanced
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