我正在尝试学习一点Scala,并且尽可能地陷入一种奇怪的错误,我可以用两种所谓的等效方式写出相同的内容,但是一次运行而另一种运行则没有。
val test_array = Array(1,2,3,4,5,6,7,8,9,10,3,4)
val it = test_array.sliding(2).toList
def iter(lst: List[Array[Int]]): List[Boolean] = lst match {
case h :: Nil => List(false)
case h :: tail => tail.map(x => x.sameElements(lst.head)) ++ iter(tail)
}
if(iter(it).contains(true)) ...
和
val test_array = Array(1,2,3,4,5,6,7,8,9,10,3,4)
val it = test_array.sliding(2).toList
def iter(lst: List[Array[Int]]): List[Boolean] = lst match {
case h :: Nil => List(false)
case h :: tail => tail.map(x => x.sameElements(h)) ++ iter(tail)
}
if(iter(it).contains(true)) ...
第一个示例运行,第二个示例抛出noSuchMethodError:scala.collection.immutable。$ colon $ colon.hd $ 1()
唯一的区别是我如何访问头部。在一种情况下,我使用解构方式,另一种我使用list.head。为什么一个运行而另一个不运行?