I\'m starting out with scala, and trying to apply the functional way to it, but I came out with bunch of nested if\\else constructions which is hard to read, and I wonder is
var parens :List[Char] = Nil
def matcher(chrs: List[Char]): Boolean = {
if (chrs.isEmpty) {
return parens.isEmpty
}
else {
chrs.head match {
case '(' => parens = '(' :: parens ;matcher(chrs.tail)
case ')' => if (parens.isEmpty) return false
else if (parens.apply(0) == '(') parens = parens.drop(1)
else return false;
matcher(chrs.tail);
case _ => matcher(chrs.tail)
}
}
}
As you can see I didn't use count because count won't work on ())(