import scala.annotation.tailrec def fib(x: Int): Int = if (x == 0 || x == 1) {x} else { fib(x - 1) + fib(x - 2) } def fibTail(x: Int): Int = { @tailrec def fibTailStep(x: Int, a: Int, b: Int): Int = { if (x == 0) {a} else fibTailStep(x - 1, b, a + b) } fibTailStep(x, 0, 1) } fib(10) fibTail(10)