I have been trying to run this code but somehow have hit the wall with 'unit mismatch, boolean expected error'. I have run through various questions on Stackoverflow but haven't found anything concrete that answers my question.
def balance(chars: List[Char]): Boolean =
{
var i = 0;
var j = 0;
if (Count(i, j) == 0){
true
}
else{
false
}
def Count(count: Int, Pos: Int): Int =
{
if (Pos == chars.length)
{
count
}
else
{
if (chars(Pos) == '(')
{
Count(count + 1, Pos + 1);
}
else
{
Count(count - 1, Pos + 1);
}
}
}
}
A code block delimited by {}
evaluates to the last expression inside it. Here, your last expression is a definition (def Count
), which evaluates to Unit
. So move the expression you expect to be evaluated to the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With