【发布时间】:2018-09-01 08:44:58
【问题描述】:
我试图理解流中的递归,这令人困惑。我的代码是这样的
val bigIntFromStream : Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: bigIntFromStream.zip(bigIntFromStream.tail).map{x =>
println(s"stream = ${bigIntFromStream} + stream.tail = ${bigIntFromStream.tail} + zipped value = ${bigIntFromStream.zip(bigIntFromStream.tail)}")
println(s"Calculating value for ${x}, {x._1} = ${x._1}, {x._2} = ${x._2}")
x._1 + x._2}
代码的输出是这样的
0 1 stream = Stream(0, 1, ?) + stream.tail = Stream(1, ?) + zipped value = Stream((0,1), ?)
Calculating value for (0,1), {x._1} = 0, {x._2} = 1
1 stream = Stream(0, 1, 1, ?) + stream.tail = Stream(1, 1, ?) + zipped value = Stream((0,1), ?)
Calculating value for (1,1), {x._1} = 1, {x._2} = 1
2 stream = Stream(0, 1, 1, 2, ?) + stream.tail = Stream(1, 1, 2, ?) + zipped value = Stream((0,1), ?)
Calculating value for (1,2), {x._1} = 1, {x._2} = 2
3 stream = Stream(0, 1, 1, 2, 3, ?) + stream.tail = Stream(1, 1, 2, 3, ?) + zipped value = Stream((0,1), ?)
Calculating value for (2,3), {x._1} = 2, {x._2} = 3
5 stream = Stream(0, 1, 1, 2, 3, 5, ?) + stream.tail = Stream(1, 1, 2, 3, 5, ?) + zipped value = Stream((0,1), ?)
Calculating value for (3,5), {x._1} = 3, {x._2} = 5
我的问题是 x 如何获得 zip 在实际值和它的尾部之间的最后一个值?如果我遗漏了什么,请告诉我
【问题讨论】:
标签: scala