I have the following piece of code (this is for my coursea assignment)
def balance(chars: List[Char]): Boolean = {
def innerBalance(chars: List[Char], count: Int): Boolean = {
if (chars.isEmpty) count == 0
if (chars.head == '(') innerBalance(chars.tail, count+1)
if (chars.head == ')') (count > 0) && innerBalance(chars.tail, count-1)
innerBalance(chars.tail, count)
}
innerBalance(chars, 0)
}
From what I can tell, it's very similar to the stew's answer on this Scala way to program bunch of if's but I don't know why the statement
if (chars.isEmpty) count == 0
always be false.
If I run a test like this
balance("".toList)
it just throws exception.
Thanks for your help. Regards,