Try to fix the loop function inside
fib so that it returns the correct values for each case in a tail-recursive way. What should the missing expressions for the trivial case and the recursive call be?
def fib(n: Int): Int = {
@annotation.tailrec
def loop(n: Int, prev: Int, cur: Int): Int =
if (n <= __ ) prev
else loop(n - __, cur, prev + cur)
loop(n, 0, 1)
}
Login in to like
Login in to comment