How to fix my Fibonacci stream in Scala

前端 未结 2 727
小蘑菇
小蘑菇 2020-12-11 04:15

I defined a function to return Fibonacci stream as follows:

def fib:Stream[Int] = {
  Stream.cons(1,
    Stream.cons(2,
      (fib zip fib.tail) map {case (x, y)          


        
2条回答
  •  感情败类
    2020-12-11 04:31

    You can do it the other way:

    
    lazy val fibs = {
      def f(a: Int, b: Int): Stream[Int] = a #:: f(b, a + b)
      f(0, 1)
    }
    

提交回复
热议问题