T
traccia
Guest
Hi, I'm studying the recursion of the functions in F #. I don't understand how this function calculates the fibonace sequence. Would someone be able to explain what are the intermediate values and how these make up the final result?
// a function to generate the Fibonacci numbers
let rec fib x =
match x with
| 1 -> 1
| 2 -> 1
| x -> fib (x - 1) + fib (x - 2)
// call the function and print the results
printfn "(fib 2) = %i" (fib 2)
printfn "(fib 6) = %i" (fib 6)
printfn "(fib 11) = %i" (fib 11)
This example, when executed, returns the following:
(fib 2) = 1
(fib 6) = 8
(fib 11) = 89
until now the only thing certain, for me, is that x = 1 or x = 2 does not trigger recursion and that x will always become smaller.
Thanks for your patience
Continue reading...
// a function to generate the Fibonacci numbers
let rec fib x =
match x with
| 1 -> 1
| 2 -> 1
| x -> fib (x - 1) + fib (x - 2)
// call the function and print the results
printfn "(fib 2) = %i" (fib 2)
printfn "(fib 6) = %i" (fib 6)
printfn "(fib 11) = %i" (fib 11)
This example, when executed, returns the following:
(fib 2) = 1
(fib 6) = 8
(fib 11) = 89
until now the only thing certain, for me, is that x = 1 or x = 2 does not trigger recursion and that x will always become smaller.
Thanks for your patience
Continue reading...