Is the standard library in OCaml that sparse?

Hi there!

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… :slight_smile:

Probably somebody can give me a hint where to search further?

Thank you in advance!

You might try Data Structures and Algorithms | OCamlverse . (After identifying the package you want to use, you can search for documentation at if the package does not have its own documentation link.)

Have you looked for packages on opam? Maybe iter would fill some of your needs?

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.

1 Like

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 :slight_smile:

1 Like

I would add that the oseq library (compatible with Seq) has more exotic functions (like permutations iirc)


Oh that´s good to know. I have a certain Common Lisp background which is very baroque :grinning:.

1 Like

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.

Just curiosity.

I tinkering with Euler 51.
I am no Mathematician, so I do it with explorative programming on the repl.

When I look at that the problem, I need all 8 digit primes with double digits:

let double-digit-primes = Int.range(10000001, 99999999, 2) |> List.filter (fun x → (is-prime x) && (has-double-digit x))

For me as a Lisp or Python programmer it feels super wrong doing this:
let double-digit-primes = List.init 45000000 (fun x → 10000001 + x * 2) |> …

I would bet that there is a fast library routine doing this for me.

With iter you could write:

Iter.(int_range_by 10000001 99999999 ~step:2 |> filter (fun x -> is_prime x && has_double_digit x))

Oh, that’s an interesting syntax!
.( - never saw something like that before…

I realize OCaml is definitely a new kind of language to me.
I go ahead with a beginners mind. :slight_smile:

I am impressed how quick people respond here and how helpful you are!

1 Like

Oh, I didn’t mean to throw funky syntax at you. The Iter.( ... ) form is shorthand syntax for a “local open”, see here.

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.

Ah, well.

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.

1 Like

I agree, the materialization of lists often hampers the performance.
So its good to know that iter helps here.

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?