【发布时间】:2022-11-24 23:39:37
【问题描述】:
初始代码是:
def nth(list: List[Int], x: Int) = ???
我试试这个:
def nth(list: List[Int], n: Int): Option[Int] =
list match {
case h :: t if n > 0 => nth(t, n - 1)
case _ => list
}
但它不起作用
谢谢你帮我我试试以前的代码
【问题讨论】:
-
case _ => list是List[Int]而不是Option[Int],您也没有处理例如 2 元素列表将被要求提供第 4 个元素的情况。
标签: scala