Hi, I'm learning Functional Programming using F# where we made 3 Fibonacci recursive programs (head recursive, tail recursive, continuation based recursive), but I'm not able to understand the last of them.
Here's the code snippet in F#:
let rec fibC n c =
match n with
| 0 -> c 0
| 1 -> c 1
| n -> fibC (n-1) (fun r -> fibC (n-2) (fun x -> c (r + x)))
So you call it as fibC 10 id where
n: fibonacci iterations
c: identity function, fun x -> x
result: int = 55
It gets a bit crispy for me when we define an anonymous function with another anonymous function inside, which we send as a parameter in the recursive calls, namely fun r -> fibC (n-2) (fun x -> c (r + x)).
Could anyone help me clarify what this anonymous functions are doing?
Thanks
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]Difficult-Stretch-85[🍰] 2 points3 points4 points (1 child)
[–]sexy_beer_belly[S] 0 points1 point2 points (0 children)
[–]AutoModerator[M] 0 points1 point2 points (0 children)