I am trying to implement the scala splitAt using pattern matching and this is what I am trying to do:
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]())
}
The compiler is complaining by saying:
<console>:15: error: unreachable code
case (currentIndex,x::y) => splitHelper(currentIndex+1,y,x::headList)
Can someone point out what I am doing wrong here and why I am getting the unreachable code error.
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…