【发布时间】:2013-02-07 02:24:32
【问题描述】:
我正在尝试在 Scala 中创建一个叉积函数,其中k 是我构建叉积的次数。
val l = List(List(1), List(2), List(3))
(1 to k).foldLeft[List[List[Int]]](l) { (acc: List[List[Int]], _) =>
for (x <- acc; y <- l)
yield x ::: l
}
但是,这段代码无法编译:
test.scala:9: error: type mismatch;
found : List[List[Any]]
required: List[List[Int]]
for (x <- acc; y <- l)
^
为什么它会认为我有一个List[Any]?显然,我正在处理的所有内容都是 Lists 或 Ints。
【问题讨论】: