## Programming Praxis – Balanced Delimiters

In today’s Programming Praxis exercise, our goal is to determine whether the delimiters in a given string are balanced. Let’s get started, shall we?

The exercise mentions that parser generators are useful for this exercise, but since the grammar isn’t very complicated I’m just going to use the straightforward method of pushing the delimiters onto a stack.

```balanced :: String -> Bool
balanced = f [] where
f bs       [] = null bs
f bs       ('\\':_:cs) = f bs cs
f (b:bs)   (c   :  cs) | lookup b pairs == Just c           = f bs cs
| elem b "'\""                       = f (b:bs) cs
f bs       (c   :  cs) | any (\(a,b) -> elem c [a,b]) pairs = f (c:bs) cs
| otherwise                          = f bs cs
pairs = [('(', ')'), ('[', ']'), ('{', '}'), ('\'', '\''), ('"', '"')]```

Some tests to see if everything is working properly:

```main :: IO ()
main = do print . and \$ map balanced ["", "abc", "()", "[[[]]]", "'('", "\\(()"]
print . and \$ map (not . balanced) ["(", "([)]", "'{'}", "\\()"]```