Erlang Nesting
-module(nesting).
-export([nesting/1]).
nesting(S) ->
case close_stack(S, []) of
[] -> 1;
_ -> 0
end.
close_stack([], Stack) -> Stack;
close_stack([$) | T], [$( | Rest]) -> close_stack(T, Rest);
close_stack([$) | _], _Stack) -> error;
close_stack([C | T], Stack) -> close_stack(T, [C | Stack]).
This treats the string like a balance counter: open parentheses add one, closing ones remove one.