Why I Like OCaml


Why I Like OCaml

Inspired by TjDevries’ blog post, I wanted to write my own post about this topic. So why do I like OCaml? What is so cool about it, and what sets it apart from other languages?

For me the short answer is: OCaml feels small, sharp, and very capable. It does not try to impress you with a thousand features. It gives you a strong type system, clean syntax, and a programming model that nudges you toward writing code that is easy to reason about. That sounds boring until you actually use it and realize how nice boring can be.

Type System

The type system is the biggest reason I like OCaml. It uses Hindley-Milner type inference, which means the compiler can often figure out the types for you without making the code noisy. You still get strong static typing, but you do not have to spell out every little thing.

That combination is excellent: the compiler catches mistakes early, but the code stays readable. When I write OCaml, I feel like the compiler is helping me think instead of blocking me.

let add x y = x + y

That tiny function already tells you a lot. The compiler knows what x and y are supposed to be, and if I try to use them incorrectly, it will complain immediately. That is the kind of feedback loop I want.

I also really like the module system. OCaml encourages you to organize code with modules and interface files (.mli), which gives you a nice way to separate implementation from API. That makes larger projects feel tidy instead of chaotic.

The type system also makes pattern matching feel powerful instead of risky. With algebraic data types, you can model your domain directly and let the compiler help you cover every case.

Functional Programming Paradigm

OCaml is a functional language first, and I think that is a feature, not a restriction. Functional programming tends to push you toward small functions, immutable data, and clear data flow. That is great when you want code that is easy to test and easy to change.

Why is functional programming so nice? Because it makes state changes explicit. Because a function that takes input and returns output is much easier to understand than a function that quietly mutates half the program. Because composition is fun.

let square x = x * x

let sum_of_squares xs =
  xs
  |> List.map square
  |> List.fold_left ( + ) 0

That kind of code feels natural in OCaml. It is concise, but not cryptic. And when you do need mutation, you can still use it. OCaml is not dogmatic about it; it just encourages better defaults.

Maybe that is also why OCaml shows up in places like compiler courses and type theory classes. It is good at teaching you how programs actually fit together. It makes you think about data first, which is usually a good thing.

Some Code Examples

I have not used OCaml for every kind of project, but it has worked well for the things I actually like building: small tools, puzzle solutions, and experiments.

For Advent of Code, OCaml is a joy. The language is fast enough, expressive enough, and pleasant enough that you can focus on the problem instead of fighting the tooling. For competitive programming style tasks, the type system helps keep you honest.

For example, parsing data feels natural:

type direction = Up | Down | Left | Right

let move = function
  | Up -> (0, 1)
  | Down -> (0, -1)
  | Left -> (-1, 0)
  | Right -> (1, 0)

That is a tiny example, but it shows the style well. The domain is encoded in the type, and the compiler makes sure every case is handled.

Could I use OCaml for a web app? Sure, probably with some effort. Would I reach for it first? Maybe not. But for CLI tools, parsers, and problem-solving code, it feels very strong. It has this nice quality where the code looks simple even when the idea is not.

And honestly, that is the main reason I keep coming back to it.

Summary

OCaml is special because it gives you a very strong type system without making every file feel heavy. It encourages a functional style that is clean and expressive. And it is one of those languages that makes you feel smarter while using it, which is always a nice bonus.

I like OCaml because it is practical, elegant, and a little underrated. It is the kind of language I enjoy learning from, and that is enough reason for me to keep using it.