Note that you can often use foldLeft or foldRight in such situations:
def fib(n: Int) = (1 to n).foldLeft((BigInt(0),BigInt(1)))((p,_)=>(p._2,p._1+p._2))._1
[change]
Another approach would be an iterator-based solution:
def fib = Iterator.iterate((0,1)){case (x,y) => (y,x+y)}.map(_._1)
This generates an infinite number of fibonacci numbers, but you can just take as much as you want from it, for example. fib.take(10).toList
source share