0

我的任务:

  1. 实现一个函数来计算Look-and-say序列。

完成后,测试成功通过:

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))
}
  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)))

我将不胜感激任何帮助。

4

1 回答 1

1

看起来您没有正确使用 LazyList 语法。您需要按如下方式使用它:

LazyList.iterate(List(BigInt(1)))(nextLine).take(4).force

iterate 的第一个参数是将在函数中传递的起始元素 - iterate 的第二个参数。拍摄需要进行多次迭代,并且需要评估结果。

于 2022-02-07T14:55:33.690 回答