I gave OCaml a try while solving a problem from the Euler project.
I am delighted to see how fast OCaml runs, but I currently struggling
to find basic standard libraries like itertools in Python. As you might
image, solving Euler problems need those kind of functions. Is it
really true, that there is no list generation like range, functions like permutation etc.?
Feels a little like 3rd. semester back in the days…
Probably somebody can give me a hint where to search further?
Did you look at the Seq module? It has many helpful functions for building and iterating, combining, splitting, etc. E.g. you should be able to build a ‘range’ function using unfold.
It’s worth noting that OCaml generally follows the practice of keeping a lean stdlib, so don’t be afraid to add packages (or stdlib overlays like Containers or Base) if you’re not interested in implementing common stuff yourself
If you care to share, I’d be curious what problems in Euler you were trying to solve, that made you feel like the OCaml standard library was insufficient. I ask b/c one of the things about OCaml is that because it’s a functional language, much of what is done with iterators in other languages, is done with map/fold/iter combinators in OCaml. But that might not be what you’re referring to, which is why I ask.
It’s a fair cop, that typically you don’t find generators of ranges: I have a couple in my “handy dandy toolbag” too, and others have pointed out that they’re present in many add-on libraries.
Now that you mention it, I also noticed the lack of such functions, and of “good iterator support” generally, when I was programming in Rust. Too often, in OCaml you have to materialize a list, then map/filter to produced another list, then again, then again, maybe finally fold to produce your final result. Where in Rust, you’d create an iterator, then transform to another iterator, etc, finally collecting into a result. But nowhere in that process was the entire list constructed all-together. So much less memory consumption.
I think that it is really worthwhile to look at iter, it does really well (especially in combination with flambda) at avoiding this sort of “materialize a list only to e.g. fold over it” situation.
It sounds like you haven’t tried to use the Seq module recently. When was the last time that you tried to use Seq and went back to materializing lists?