here is 2 functions that calculate the fibbonaci number, they both work correctly, but are written in a different way.
which one would you consider better, more efficient, code more understandable ?
let fibe n =
let rec loop acc1 acc2 n =
match n with
| n when n = 0I -> acc1
| x -> loop acc2 (acc1 + acc2) (x - 1I)
loop 0I 1I n
let myfib n =
if n = 0I then 0I
else if n = 1I then 1I
else
let rec loop i f s =
match i with
| x when x = n -> f+s
| x when x < n -> loop (i+1I) s (s+f)
loop 2I 0I 1I
Talking of clarity, both your functions IMO are unnecessary cluttered and use inadequate language mechanisms for the purpose.
Generating Fibonacci is an ideal fit for expressing via
unfold, like below:How may you make it further shorter and clearer?
UPDATE: as the question’s author considers the ability to operate with Fibonacci members having sequence numbers of
bigintas important, the above snippet definitely can be adopted to this requirement, although getting less straightforward: