我想使用 fill 和 tabulate 创建一个列表,其中包含从 0 到 -1 的方格作为包含重复项的列表。
类似这样的事情:
squareNtimes(5)
//> res0: List[List[Int]] = List(List(0, 0, 0, 0, 0), List(1, 1, 1, 1, 1), List(4, 4, 4, 4, 4), List(9, 9, 9, 9, 9), List(16, 16, 16, 16, 16))
我写了这个函数:
def squareNtimes(count: Int) = List.fill(count)(List.tabulate(count)(n => n * n))
但是输出不一样:
//> res0: List[List[Int]] = List(List(0, 1, 4, 9, 16), List(0, 1, 4, 9, 16), List(0, 1, 4, 9, 16), List(0, 1, 4, 9, 16), List(0, 1, 4, 9, 16))
有人知道我犯了什么错误吗?
最佳答案
你已经很接近了,我认为这应该可行:
List.tabulate(count)(n => List.fill(count)(n * n))
关于list - 使用 Scala 创建一个列表,其中包含从 0 到 count – 1 的方 block 作为包含重复项的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36653464/