0

I would like to have this suite computed :

h(-2)=0
h(-1)=1
h(i)=a(i)*h(i-1)+h(i-2), for i>=0

(a is given; for my tests I use a=(1,1,2), it has only 3 terms)

I think the best way is to use streams, so I wrote:

val h:Stream[Int]=0#::1#::(h.zipWithIndex.zip(h.tail).zip(h.tail.tail)).map{case(((a,i),b),c)=>{
    liste_a(i-2)*b+a
}}
def h2(i:Int)=h(i+2)

but I have a ...stack overflow error, and I don't know where it comes from.

4

1 回答 1

2

因为这个,你有一个堆栈溢出:h.tail.tail。这将尝试计算 的第三个元素h(因为流在它们的 中是严格的head),并且计算需要使用的表达式h.tail.tail,从而导致无限循环。

于 2013-10-12T00:07:04.450 回答