Scala always returning true....WHY?
Posted
by
jhamm
on Stack Overflow
See other posts from Stack Overflow
or by jhamm
Published on 2012-09-28T21:29:25Z
Indexed on
2012/09/28
21:37 UTC
Read the original article
Hit count: 196
scala
I am trying to learn Scala and am a newbie. I know that this is not optimal functional code and welcome any advice that anyone can give me, but I want to understand why I keep getting true for this function.
def balance(chars: List[Char]): Boolean = {
val newList = chars.filter(x => x.equals('(') || x.equals(')'));
return countParams(newList, 0)
}
def countParams(xs: List[Char], y: Int): Boolean = {
println(y + " right Here")
if (y < 0) {
println(y + " Here")
return false
} else {
println(y + " Greater than 0")
if (xs.size > 0) {
println(xs.size + " this is the size")
xs match {
case xs if (xs.head.equals('(')) => countParams(xs.tail, y + 1)
case xs if (xs.head.equals(')')) => countParams(xs.tail, y - 1)
case xs => 0
}
}
}
return true;
}
balance("()())))".toList)
I know that I am hitting the false branch of my if statement, but it still returns true at the end of my function. Please help me understand. Thanks.
© Stack Overflow or respective owner