我想学习斯卡拉,我是一个新手。我知道这不是最佳的功能代码,并欢迎任何人都可以给我的建议,但我想明白为什么我一直对此功能感到满意。斯卡拉总是回归真实....为什么?
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)
我知道,我打我的if语句的假分支,但它仍然在我的函数结束时返回true。请帮助我理解。谢谢。
因为你在方法的末尾写了true。 – phant0m
我也在使用#progfun,并且我认为我的家庭作业解决方案中没有多少代码行。 –