Haskell Nesting
nesting :: String -> Int
nesting s
| null s = 1
| otherwise = go s []
where
go [] stack = if null stack then 1 else 0
go (c : cs) stack
| c == ')' = pop cs stack
| otherwise = go cs (c : stack)
pop cs (top : rest)
| top == '(' = go cs rest
pop _ _ = 0
This treats the string like a balance counter: open parentheses add one, closing ones remove one.