I wrote the following program to check strings for balanced parenthesis:
isBalanced xs = isBalanced\' xs []
isBalanced\' [] [] = True
isBalanced\' [] _ = False
As Henning said, parser combinators would work for this. Here's an example using Parsec:
import Text.Parsec
grammar = many braces >> return ()
where braces = choice [ between (char '(') (char ')') grammar
, between (char '[') (char ']') grammar
, between (char '{') (char '}') grammar
]
isBalanced :: String -> Bool
isBalanced input = case parse (grammar >> eof) "" input of
Left _ -> False
Right _ -> True