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
It seems we are attending the same course. my solution:
def balance(chars: List[Char]): Boolean =
doBalance(chars, 0) == 0;
def doBalance(chars: List[Char], parenthesisOpenCount: Int): Int =
if(parenthesisOpenCount <0) -100;
else
if(chars.isEmpty) parenthesisOpenCount
else
chars.head match {
case '(' => return doBalance(chars.tail, parenthesisOpenCount+1)
case ')' => return doBalance(chars.tail, parenthesisOpenCount-1)
case _ => return doBalance(chars.tail, parenthesisOpenCount)
}