I tried to create a lazy infinite Fibonacci stream using Scala, here are my two solutions.



The first one is a naive one using inner function:





def fib:Stream[Long]={

def fibInner(f0:Long, f1:Long):Stream[Long]={

Stream.cons(f0, fibInner(f1, f0+f1))

}

fibInner(1,2)

}





def fib2:Stream[Long]={

Stream.cons(1, Stream.cons(2, fib2.zip(fib2.tail).map(x=>x._1+x._2)))

}



The second one is ported from Haskell:Although both of them works but the second one is way slower than the first one.