我想學習斯卡拉,我是一個新手。我知道這不是最佳的功能代碼,並歡迎任何人都可以給我的建議,但我想明白爲什麼我一直對此功能感到滿意。斯卡拉總是迴歸真實....爲什麼?
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,並且我認爲我的家庭作業解決方案中沒有多少代碼行。 –