我的任务是:
这样做了,测试就成功通过了:
import scala.collection.immutable.LazyList
import scala.language.implicitConversions
import scala.collection.mutable.ListBuffer
object CountAndSay extends App {
def nextLine(currentLine: List[BigInt]): List[BigInt] = {
...
println(nextLine(List(1, 2, 1, 1)) == List(1, 1, 1, 2, 2, 1))
println(nextLine(List(1, 1, 1, 2, 2, 1)) == List(3, 1, 2, 2, 1, 1))
println(nextLine(List(3, 1, 2, 2, 1, 1)) == List(1, 3, 1, 1, 2, 2, 2, 1))
}val funSeq: LazyList[List[Int]] = ...
println(funSeq(5) === List(3, 1, 2, 2, 1, 1))要使用嵌套列表创建LazyList,我希望使用LazyList.iterate。LazyList.iterate描述,但我得到一个无法解决重载方法“迭代”错误的方法:
val funSeq: LazyList[List[BigInt]] = LazyList.iterate(List(1), nextLine(List(1)))我很感谢你的帮助。
发布于 2022-02-07 14:55:33
您似乎没有正确地使用LazyList语法。您需要按以下方式使用它:
LazyList.iterate(List(BigInt(1)))(nextLine).take(4).force迭代器的第一个参数是开始元素,它将在迭代器的函数-第二参数中传递。需要进行几次迭代,而强制则需要评估结果。
https://stackoverflow.com/questions/71017811
复制相似问题