Stack overflows and recursive sequence expressions F#

I have a sequence expression like so:

let fibSeq = 
    let rec fibSeq' a b = 
        seq { yield a
              yield! fibSeq' b (a + b) }
    fibSeq' 1 1

Now even for large numbers, this will not generate a stack overflow. I'm wondering why, it seems to me that to generate n Fibonacci numbers with this sequence expression each recursive call would need to return back to the caller eventually to "fold" itself into the sequence. Is there some sort of optimization going on behind the scenes here?


是的,它被称为“尾巴呼叫优化”请参阅:http://blogs.msdn.com/b/chrsmith/archive/2008/08/07/understanding-tail-recursion.aspx此外,Seq很懒惰,所以其第500名成员将直到你不需要在你的程序中访问它就可以进行评估,例如:

let elm = Seq.nth 500 fibSeq
链接地址: http://www.djcxy.com/p/80620.html

上一篇: 尾巴。 ILAsm中的前缀 - 任何使用示例?

下一篇: 堆栈溢出和递归序列表达式F#