Question: 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) =
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
write the code in SMLNJ
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
