DEV Community

loading...
Cover image for How To Learn OCaml

How To Learn OCaml

Sophia Brandt
former tax officer turned programmer
Originally published at rockyourcode.com on ・3 min read

A starting point for learning OCaml - a functional, type-safe, expressive, and battle-proven language

Why OCaml?

OCaml is an open-source functional, statically typed, and mature language. If you want to learn such a paradigm, OCaml is far more beginner-friendly than Haskell.

OCaml has a blazingly fast compiler and excellent type inference. It offers exhaustive pattern-matching, which makes your programs concise and a joy to work with.

I've come to OCaml from ReasonML, a JavaScript-like syntax, and tool-chain for OCaml.

But as ReasonML is OCaml, you should know how OCaml works.

This guide is for people who already know at least one programming language, not for total beginners.

Beginner's Guide to OCaml Books

Start by reading the blog post by Alex Clemmer. It provides an overview of novice-friendly books and tools.

To make it short, he recommends Real World OCaml (free HTML version) and OCaml from the Very Beginning.

OCaml Website

The official website has a dedicated section for learning resources.

Cornell's Book for Course 3110 (Learning a Language)

(edit: Michael Kohl pointed this one out to me.)

The free text book covers a variety of topic concerning functional programming, and OCaml.

A Practical Approach

To get started quickly, read the OCaml section of Learn X in Y minutes to get the big picture.

Then head over to the Jane Street OCaml Workshop and clone their repository.

Check out Shawn Wang's Ocaml Speedrun! blog post on dev.to for solutions to the workshop.

Install OCaml and OPAM, install Dune (via Opam), and go through the workshop.

Supplement your learnings by reading the books mentioned above or by going through the free MOOC Introduction to Functional Programming in OCaml.

Exercism.io also offers an OCaml track.

Editor Tools

OCaml uses Opam for package management. You have to install it and then run the following command from your terminal:

opam init
Enter fullscreen mode Exit fullscreen mode

VS Code comes with several plugins for OCaml.

reason-vscode should suffice for most use cases. It comes with a built-in language server that offers autocomplete, hover information, and more.

Make sure to use a compatible OCaml version. The latest Ocaml version (4.09.0) doesn't work yet; you have to downgrade to 4.07.0.

I've found a guide for VS Code and Ocaml from 2018, which could be useful.

If you use Vim, Merlin provides a better experience.

I'll write a guide about my setup with NeoVim soon.

Links

Discussion (3)

Collapse
citizen428 profile image
Michael Kohl • Edited

Let me offer one more resource, Learning a Language, a text book for Cornell's CS 3110 course which uses OCaml. Also while originally developed for Reason, esy slowly seems to be gaining some traction in the OCaml community too, or at least in my Twitter bubble.

Collapse
sophiabrandt profile image
Sophia Brandt Author

Thank you very much, Michael! That looks like a concise tour on OCaml. Very helpful.

Collapse
alexleduc76 profile image
Alexandre Leduc

The problem with ReasonML, which I attempted to learn a while back, is that it doesn't have nearly as much documentation as OCaml (OCaml even has a good book on it, published by O'Reilly) so you eventually realize that the easier learning path is to learn OCaml first. But then why learn ReasonML after that?

I eventually gave up on learning a "real" functional language. Instead I use things like Ramda JS library and Redux, which enforce a lot of good functional patterns like immutability and function composition, without having to leave the comfort of a language I know well.