我正在尝试使用模式匹配来实现 scala splitAt,这就是我想要做的:
def split[T](someIndex:Int,someList:List[T]):(List[T],List[T]) = {
def splitHelper[T](currentIndex:Int,someList:List[T],headList:List[T]):(List[T],List[T])= {
(currentIndex,someList) match {
case (someIndex,x::tail) => (x::headList,tail)
case (currentIndex,x::y) => splitHelper(currentIndex+1,y,x::headList)
case _ => (headList,headList)
}
}
splitHelper(0,someList,List[T]())
}
编译器抱怨说:
<console>:15: error: unreachable code
case (currentIndex,x::y) => splitHelper(currentIndex+1,y,x::headList)
有人可以指出我在这里做错了什么以及为什么我会收到无法访问的代码错误。
谢谢