試圖創建一個遞歸函數來打印數組的最高元素。遞歸方法不起作用。需要; ?
它說,它需要else maxi=xs.head
前;
和}
後max(xs.tail)
我沒想到斯卡拉採用半冒號,什麼時候應該使用它們,什麼是其他一些基本的語法規則。
var maxi = 0
def max(xs: List[Int]): Int = {if (xs.isEmpty) throw new java.util.NoSuchElementException()
else if (xs.tail.isEmpty) maxi
else if (xs.tail.head > xs.head) maxi = xs.tail.head
max(xs.tail)
else maxi=xs.head
max(xs.tail)
}
我同意不使用的可變全局狀態('maxi'),當它在暗示說,作爲隱含在演習中指定「您可能需要定義一個輔助手段」。 –