使用Scala Streams跟踪斐波那契计算的执行

时间:2014-01-02 21:52:46

标签: scala stream lazy-evaluation fibonacci

我是一个函数式编程/ scala新手。我一直在努力让我的脑袋围绕下面的代码片段和输出产生。

def fib:Stream[Int] = {
  Stream.cons(1,
    Stream.cons(2,
      (fib zip fib.tail) map {case (x, y) => println("%s + %s".format(x, y)); x + y}))
}

输出跟踪:

scala> fib take 4 foreach println

1
2
1 + 2
3
1 + 2  <-- Why this ?????
2 + 3
5

我不明白如何评估1 + 2来计算结果5。 理论上,我确实理解def应该强制重新计算fib,但是我无法找到执行跟踪中可能发生的位置。

我想通过我的理解来帮助你们。

Output( My understanding):

1  
This is the head, trivial

2  
This is the tail of the first Cons in Cons( 1, Cons( 2, fn ) ). Trivial.

1 + 2
(fib zip fib.tail) map {case (x, y) => println("%s + %s".format(x, y)); x + y}))
first element of fib is 1
first element of fib.tail is 2
Hence 1 + 2 is printed.

The zip operation on the Stream does the following
 Cons( ( this.head, that.head), this.tail zip that.tail ) # this is fib and that is fib.tail. Also remember that this.tail starts from 2 and that.tail would start from 3. This new Stream forms an input to the map operation.

The map operation does the following 
cons(f(head), tail map f ) # In this case tail is a stream defined in the previous step and it's not evaluated.

So, in the next iteration when tail map f is evaluated shouldn't just 2 + 3 be printed ? I don't understand why 1 + 2 is first printed 

:( :( :(

有什么明显的东西我不见了吗?

1 个答案:

答案 0 :(得分:1)

https://stackoverflow.com/a/20737241/3189923中提出的Fibonacci编码,其中添加了详细信息用于跟踪执行,

val fibs: Stream[Int] = 0 #:: fibs.scanLeft(1)((a,b) => {
  println(s"$a + $b = ${a+b}")
  a+b
})

然后,例如,

scala> fibs(7)
1 + 0 = 1
1 + 1 = 2
2 + 1 = 3
3 + 2 = 5
5 + 3 = 8
8 + 5 = 13
res38: Int = 13