Learn OCaml

by smartmicon 6/27/25, 8:36 PMwith 78 comments
by mtlynchon 6/28/25, 12:16 AM

As someone interested in learning OCaml, this felt like a pretty inaccessible introduction.

Having seen "A tour of Elm,"[0] I really prefer that style. The left-hand side (what English readers read first) is an explanation of the concept, then the right side is the code, and the explanation gives you enough details to complete the code.

This introduction doesn't really explain anything, as I guess it assumes you've learned OCaml elsewhere and are just here to practice.

I tried the first exercise, and it felt more like a math problem than an exercise to teach a programming concept:

>Suppose that a variable x exists and is an integer.

>Define a variable x_power_8 that uses three multiplications to calculate x to the power of 8. The only function you are allowed to call is the (*) operator.

>Hint: use auxiliary variables.

So, at first I thought I was supposed to just call multiply eight times, and then I realized that they said you can only call multiply three times. So, you're supposed to do let a = x * x; let b = a * a; let x_power_8 = b * b. But that feels really contrived to me and not like anything I'd write in a real application, even a toy one. If the idea is teaching variables, why not just ask me to declare a variable that represents x plus 1?

[0] https://a-tour-of-elm.axelerator.de/#JSFunctions

by transputeon 6/28/25, 3:11 AM

"Thoughts on Rust vs. OCaml" (2020), https://news.ycombinator.com/item?id=24223018

  At Rust Chipotle, they have strict rules about the ingredients for your burrito. "White rice with medium salsa, sir? Absolutely not!". You see, medium salsa only goes with brown rice, and you also need to have beans or nothing works. Under no circumstances will they allow you to construct the burrito you think you want, no matter how much you think you want it.

  Meanwhile, at OCaml Chipotle you can have whatever you like, and it always turns out awesome. But once a month you go for lunch and they'll refuse to make you a bowl, refuse to tell you why, and refuse to let you leave. And when you try to get help from a passerby after being trapped in the store, you realize there's nobody nearby who you can ask.

by __rito__on 6/28/25, 6:55 AM

I highly recommend this resource- OCaml Programming: Correct + Efficient + Beautiful [0]. It is very friendly, fund, and informative. It is one of the proverbial 'mind expanding' stuff.

[0]: https://cs3110.github.io/textbook/cover.html

by rpedenon 6/28/25, 2:59 PM

I like OCaml a lot - but I think I like F# a little more. They're very similar, since F# as essentially Ocaml running on the .NET VM.

I know some people dislike the fact that F# lacks OCaml's functors, but I can see why they weren't included. Due the the way F# integrates .NET classes/objects, I can accomplish more or less the same thing that way. In some ways I prefer it - a class/type full of static methods has the same call syntax as a module full of functions, but gives me the option of overloading the method so it'll dispatch based on argument types. Having everything that's iterable unified under IEnumerable/Seq is nice, too.

Having said all that, I still enjoy OCaml a ton. One thing I wish I could have is F#'s updated lightweight syntax brought over to OCaml. I think ReasonML is great, but after using it for a while I realized that what I really want isn't OCaml that looks more like JavaScript. What I want is OCaml that looks like OCaml, but a little cleaner. F# gives me that, plus, via Fable, compilation to JS, TypeScript, Python, and Rust. And via the improved native AOT compilation in .NET 9, I can build fast and reasonably small single-file executables.

Despite all that, I still try to dive in OCaml whenever it's a decent fit for a problem for the problem I'm trying to solve. Even if it's a little quirky sometimes, it's fun.

by aryonocoon 6/28/25, 6:05 AM

OCaml is nice. I like it a lot.

But arguably F# (whose original name was OCaml.NET) is underrated. It improves on OCaml in a number of areas (better pattern matching, computation expressions, namespace support) but more importantly, F# solves OCaml’s biggest problems: lack of decent libraries and subpar tooling.

With F# you get instant access to thousands of high quality NuGet packages which includes everything from fast web servers to highly performant json deserialisation, and you can use some of the best IDEs, tooling and debugger around (JetBrains Rider is outstanding IMO).

F# suffers from the Microsoft association unfortunately, even though Microsoft barely cares about it and provides minimal support for it, most good things about F# are developed and maintained by the community. But the perception exists, and many think it’s still Windows only

I’ve recently been doing a lot of F# development, I do my work on a Mac, using Rider and OCI dev containers, and the code is deployed on Kubernetes, and honestly I don’t remember the last time I enjoyed a programming language and development tooling so much.

by hyper57on 6/27/25, 10:25 PM

Loved using OCaml for a compiler course at uni when I was a student. But I've always felt that the tooling side is pretty rough, especially on Windows. Opam recently added Windows support, but it involves installing MinGW, and when following the official docs https://ocaml.org/docs/installing-ocaml#install-platform-too... the process breaks down with an error when trying to install utop due to a path separator error, which one has to fix manually (at least that was the case last time I tried). By comparison, installing Python or Rust on Windows is a breeze.

by uncircleon 6/28/25, 9:37 AM

When I first looked at OCaml 15 years ago, it felt so complex and couldn’t wrap my head around it.

Now after almost a decade with Elixir, used to functional patterns and immutable data access, I followed a course on compiler construction in OCaml [1] and it felt surprising easy and straight forward, while my attempts using regular imperative languages feel like I’m wasting time on bureaucracy and boilerplate (i.e. in C, Rust or Python) - pattern matching is such an amazing feature.

1: https://cs3110.github.io/textbook/cover.html

by b0a04glon 6/27/25, 10:11 PM

picked up ocaml back when prepping for some interview round, didn’t expect much just wanted the functional knowledge. but later used it for advent of code and it just worked so clean. pattern matching, recursion, immutabilitty.. fits those problems naturally. ended up liking the language way more than planned.

by another_twiston 6/28/25, 5:13 AM

Is OCaml still worth learning these days ? Feels like plenty of languages are evolving quite well and most things can be done in imperative langs like Go/Java with pretty concise code and certainly better perf.

by pmbanugoon 6/28/25, 6:53 PM

Is ocaml worth learning today?

I mostly work with JS and last month picked up learning Zig and building things with it. Zig ecosystem is still early but fun to play around, plus the community folks are helpful with answering my noob questions

by chromatinon 6/28/25, 1:38 PM

Do these exercises go with a book?

The only (obvious) option is to begin solving problems.

If someone does not already _know_ OCaml, I fail to see how this is a way to learn.

A better title might be "Practice OCaml"

by fr4nkron 6/28/25, 1:31 AM

Very nice site, but it seems to expect you to be following along with some other resource. The exercises each have links under the details tab, but the links are broken, and I cannot find the web pages they are supposed to be linking to.

by nine_kon 6/28/25, 3:52 AM

BTW how about learning to use Reason, an OCaml compiler fronted with an alternative (and arguably nicer) syntax? Is it popular enough to be worth creating a "tour" app?

by BeetleBon 6/28/25, 7:24 AM

This seems a site geared towards programming exercises, and not actually teaching OCaml.

by lucaslazaruson 6/28/25, 5:07 AM

Is this Jane Street propaganda?

by luxurytenton 6/27/25, 11:29 PM

If I learned OCaml, what type of prospects would I have?

Fairly seasoned generalist, mostly writing Go these days. Lots of plumbing with LLMs etc.

Would love to learn something new but am driven by a goal in mind (ie OCaml exposes me to "X industry")

Is that a thing?