小编piy*_*thi的帖子

用于vararg的Scala模式匹配

我只是想在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)
    }
  }
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试调用此方法时

concat(List(1,2,3),List(2,3,4),List(4,5,6),List(6,7,8))
Run Code Online (Sandbox Code Playgroud)

我收到错误

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)
Run Code Online (Sandbox Code Playgroud)

谁能解释我在这里做错了什么?提前致谢

scala pattern-matching

3
推荐指数
1
解决办法
960
查看次数

标签 统计

pattern-matching ×1

scala ×1