我只是想在 scala 上做一些实践,并尝试自己实现 List.concat 函数。这是代码
def concat[A](lists : Traversable[A]*):List[A]={
println("concat called")
lists match {
case Nil => Nil
case x :: Nil => (x :\ List.empty[A])((elem,list)=> elem::list)
case x:: xs => (x :\ concat(xs:_*))((elem,list)=> elem :: list)
}
}
但是当我尝试调用这个方法时
concat(List(1,2,3),List(2,3,4),List(4,5,6),List(6,7,8))
我收到错误
Exception in thread "main" scala.MatchError: WrappedArray(List(1, 2, 3), List(2, 3, 4), List(4, 5, 6), List(6, 7, 8)) (of class scala.collection.mutable.WrappedArray$ofRef)
有人可以解释我在这里做错了什么吗?
提前致谢