次のコードがあります(これは私のコース課題用です)
def balance(chars: List[Char]): Boolean = {
def innerBalance(chars: List[Char], count: Int): Boolean = {
if (chars.isEmpty) count == 0
if (chars.head == '(') innerBalance(chars.tail, count+1)
if (chars.head == ')') (count > 0) && innerBalance(chars.tail, count-1)
innerBalance(chars.tail, count)
}
innerBalance(chars, 0)
}
私が知る限り、これはScala で多数の if をプログラムする方法に関するシチューの回答と非常によく似てい ますが、なぜステートメントが
if (chars.isEmpty) count == 0
常に偽になります。
このようなテストを実行すると
balance("".toList)
例外をスローするだけです。
ご協力いただきありがとうございます。よろしく、