Question: 2. Modify the function below to print out the value of x, fibonacci(x-1) and fibonacci(x-2) in every iteration of the function call: fun fibonacci (x:int)
2. Modify the function below to print out the value of x, fibonacci(x-1) and fibonacci(x-2) in every iteration of the function call:
fun fibonacci (x:int) =
if (x = 0 orelse x = 1)
then x
else fibonacci(x-1) + fibonacci(x-2)
The expression
fibonacci 5;
would produce the output:
x = 1
x = 0
x = 2 f(x-1) = 1 f(x-2) = 0
x = 1
x = 3 f(x-1) = 1 f(x-2) = 1
x = 1
x = 0
x = 2 f(x-1) = 1 f(x-2) = 0
x = 4 f(x-1) = 2 f(x-2) = 1
x = 1
x = 0
x = 2 f(x-1) = 1 f(x-2) = 0
x = 1
x = 3 f(x-1) = 1 f(x-2) = 1
x = 5 f(x-1) = 3 f(x-2) = 2
val it = 5 : int
Step by Step Solution
There are 3 Steps involved in it
To modify the given Fibonacci function to print out the value of x fibonaccix1 and fibonaccix2 for e... View full answer
Get step-by-step solutions from verified subject matter experts
