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
My solution for this
def balance(chars: List[Char]): Boolean = {
var braceStack = new Stack[Char]()
def scanItems(strList:List[Char]):Boolean = {
if(strList.isEmpty)
braceStack.isEmpty
else{
var item = strList.head
item match {
case '(' => braceStack.push(item)
scanItems(strList.tail)
case ')'=> if(braceStack.isEmpty){
false
}
else {
braceStack.pop
scanItems(strList.tail)
}
case _ => scanItems(strList.tail)
}
}
}
scanItems(chars)
}