I\'m working on some code to balance parenthesis, this question proved most useful for the algorithm.
I implemented it in my first language (PHP) but I\'m learning Scala
val myStack = new Stack[Char]
def balance(chars: List[Char]): Boolean = {
def processParanthesis(x: Char, a: List[Char]): Stack[Char] = {
if (x == '(') {
myStack.push('(');
} else if (x == ')') {
if (!myStack.empty())
myStack.pop();
else
myStack.push(')');
}
if (a.length == 0)
return myStack;
else
return processParanthesis(a.head, a.tail);
}
return processParanthesis(chars.head, chars.tail).empty();
}