Conversation
fibonacci/ocaml/code.ml
Outdated
| let rec aux a b n = | ||
| if n = 0 then a | ||
| else aux b (a + b) (n - 1) | ||
| in |
There was a problem hiding this comment.
I don't know Ocaml. Does this match the other naive implementation from the other languages?
There was a problem hiding this comment.
I do not use functional languages that much but this program looks exactly like if someone asked me to implement Fibonacci series with naive approach. The only difference is that we go from bottom to top but I cannot imagine how to do it the other way using functional paradigm
There was a problem hiding this comment.
This is a recursive implementation the same as the other but with a difference. It's tail call recursion. So OCaml compiler can optimize in an extreme way such type or recursion.
So, it's recursion, no caching is done, and the good performance comes from the use of tail calls.
But for the sake of comparison, I'll change it to "non tail call"recursion.
There was a problem hiding this comment.
Changed. It's important to note that tail call recursion is always preferred (I guess in any language) in OCaml.
|
Just adding my 2¢ in passing, but using tail-recursion (TCO) in OCaml is something taught and emphasized from the very beginning. The current code (with TCO removed) is simply never done, it's not an accurate representation of real OCaml code. The original version was correct because, yes, we modify every recursive algorithm to make them tail-recursive whenever possible. That's just how we do it. From the README:
Recursion is one of OCaml's strengths thanks to TCO and real-life code uses TCO as much as possible. It's a fundamental aspect of the language, so let's show it off! EDIT: I think you should also use |
|
Sadly the "tail-recursive" version is |
|
I think this looks compliant, but it is outdated and needs to be rewritten to target the current benchmark runner. See the README about this, and also #371. |
No description provided.