How To Learn OCaml
Tags: ocaml, beginners
Published: 2019-11-09 20:21:07

---
title: 'How To Learn OCaml'
cover_image: https://images.unsplash.com/photo-1517226701416-2a650991cc3c?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=420&ixid=eyJhcHBfaWQiOjF9&ixlib=rb-1.2.1&q=80&w=1000
tags: ocaml, beginners
cover_image: https://images.unsplash.com/photo-1555162215-dc01fcc052ec?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=420&ixid=eyJhcHBfaWQiOjF9&ixlib=rb-1.2.1&q=80&w=1000
series: 'How to Learn OCaml'
canonical_url: 'https://www.rockyourcode.com/how-to-learn-ocaml/'
published: true
---


> 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](https://reasonml.github.io/), 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][beginnersguide]. It provides an overview of novice-friendly books and tools.

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

## OCaml Website

The official website has a dedicated section for [learning resources][officialocaml].

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

(edit: [Michael Kohl](https://dev.to/citizen428) pointed this one out to me.)

The **[free text book][cornell]** 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][learnxiny]** to get the big picture.

Then head over to the **[Jane Street OCaml Workshop][jsworkshop]** and clone their repository.  
Check out Shawn Wang's **[Ocaml Speedrun!][swyxocaml]** blog post on dev.to for solutions to the workshop.

[Install OCaml and OPAM](https://ocaml.org/docs/install.html), [install Dune (via Opam)](https://github.com/ocaml/dune), 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][mooc]**.

[**Exercism.io** also offers an OCaml track](https://exercism.io/tracks/ocaml).

## Editor Tools

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

```bash
opam init
```

**VS Code** comes with several [plugins for OCaml](https://marketplace.visualstudio.com/search?term=ocaml&target=VSCode&category=Programming%20Languages&sortBy=Relevance).  
[reason-vscode](https://marketplace.visualstudio.com/items?itemName=jaredly.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](https://stackoverflow.com/questions/40898292/how-to-install-a-specific-version-of-ocaml-compiler-with-opam).

I've found a guide for [VS Code and Ocaml from 2018](https://www.cosmiccode.blog/blog/vscode-for-ocaml/), which could be useful.

If you use **Vim**, [Merlin](https://cedlemo.github.io/nvim/ocaml/2018/05/11/Nvim-OCaml-ide.html) provides a better experience.

I'll write a [guide about my setup with NeoVim soon](https://www.rockyourcode.com/setup-o-caml-with-neo-vim).

### Links

- [Ocaml Website](https://ocaml.org/)
- [Beginner's guide to OCaml beginner's guides.][beginnersguide] by Alex Clemmer
- [OCaml Speedrun!][swyxocaml] by Shawn swyx Wang
- [Learn X in Y Minutes: OCaml][learnxiny]
- [Cornell CS 3110][cornell]

[beginnersguide]: http://blog.nullspace.io/beginners-guide-to-ocaml-beginners-guides.html
[ocamlbeginning]: http://ocaml-book.com/
[realworldocaml]: https://realworldocaml.org/
[officialocaml]: https://ocaml.org/learn/
[learnxiny]: https://learnxinyminutes.com/docs/ocaml/
[jsworkshop]: https://github.com/janestreet/learn-ocaml-workshop
[swyxocaml]: https://dev.to/swyx/ocaml-speedrun-3f7g
[mooc]: https://www.fun-mooc.fr/cours/#search?query=ocaml
[opam]: https://opam.ocaml.org/doc/Install.html
[cornell]: https://www.cs.cornell.edu/courses/cs3110/2019sp/textbook/