Skip to content

Conversation

@juliojgd
Copy link

No description provided.

let rec aux a b n =
if n = 0 then a
else aux b (a + b) (n - 1)
in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know Ocaml. Does this match the other naive implementation from the other languages?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed. It's important to note that tail call recursion is always preferred (I guess in any language) in OCaml.

Copy link
Contributor

@botantony botantony left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it works

@SGrondin
Copy link

SGrondin commented Dec 4, 2024

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:

Emphasizes function call overhead and recursion.

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 -O3 and any other applicable performance compilation flags.

@ghost
Copy link

ghost commented Dec 21, 2024

Sadly the "tail-recursive" version is O(N) and not O(2^N)

@PEZ
Copy link
Collaborator

PEZ commented Feb 9, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants