Learning
Beginner Topics
Some motivations for using OCaml can be found here, here, here and here.
- The quickstart page to get you up and running as fast as possible.
- The OCaml website is a great resource for learning.
- OCaml cheat sheet
- Play around with OCaml online
- The Beginner’s Guide to OCaml Beginner’s guides
- Learn X in Y minutes where X=OCaml.
- Operator lookup: fun and useful site to look up the function of different OCaml operators.
- ocamlsyntax.com: another OCaml cheat sheet.
- Functional Programming with OCaml
- OCaml file types and filename extensions
- The FAQ: a good place to find answers to questions about OCaml.
- Real World OCaml: A free online book, and a great introduction to OCaml.
- Links to OCaml Q&A around the web.
- Using the OCaml toplevel/REPL
- Article on algebraic data types
- Python to OCaml: a retrospective: Blog entries about rewriting 0Install in OCaml instead of python.
- Functional Alternatives to Inheritance: Beginner-friendly look at alternatives to OOP inheritance.
Setting Up Your Editor for OCaml
OCaml can be edited conveniently with many different editors. See editor setup and code tools.
Using the OCaml Build Tools
- Quick Start an OCaml project with Dune
- Project showing how to use OPAM with/without dune
- Project showing how to use Dune in different configurations
- Project template using Dune, ocaml-odoc, Alcotest, and ocamlformat with a quick setup script
- OPAM for npm/yarn users
- A sample Dune project
- OPAM 2.0 tips: Local opam installs, pinning, and lock files.
Best Practices
Several guidelines for best practices have been established by the community. See Best Practices.
Videos
- YouTube series that accompanies the Cornell course (see in Books).
- OCaml Tutorial series (2016)
- Beginner OCaml Tutorial series (2019)
- Programming with OCaml series (2022)
Books
- Cornell OCaml Textbook (free online): Excellent free online book, covering both beginner and advanced topics.
- OCaml from the Very Beginning by J. Whitington (free online): A book for both new programmers and experienced programmers eager to explore functional languages such as OCaml.
- Real World OCaml by Y. Minsky, A. Madhavapeddy and J. Hickey (free online):
Functional programming for the masses.
A book that discusses both beginner and advanced topics.
Note that the book utilizies Jane Street libraries, but the material can be applied to non-Jane Street libraries as well.
- RWO-lwt:
translation of the Jane Street concurrency
Async
library code examples in the book to use thelwt
library.
- RWO-lwt:
translation of the Jane Street concurrency
- How to Think Like a (Functional) Programmer by Allen Downey and Nicholas Monje (free online): An introductory programming textbook using OCaml. It is a modified version of Think Python by Allen Downey. This book is intended for newcomers to programming and also those who know some programming but want to learn programming in the function-oriented paradigm, or those who simply want to learn OCaml.
Online Exercises for Beginners
- 99 problems: The classic 99 (Prolog) problems1, with solutions in OCaml.
- Rosetta Code
- OCaml at Exercism:
- OCaml at Codewars
Beginner Online Courses
- Introduction to Functional Programming in OCaml
- Cornell University: Data Structures and Functional Programming
- Princeton University: Functional programming in OCaml
- University of Illinois Course on OCaml and Functional Programming
Intermediate Topics
- Tips and Tricks
- BigArray: A high-performance array for interacting with non-OCaml programs.
- ocamlunix: Great introduction to systems programming in OCaml
- Higher-Rank Polymorphism in OCaml: How to activate higher-rank polymorphism in OCaml. Old, but still relevant.
- Survey of OCaml programs and their usage of mutable state
Multicore
Parallel multicore OCaml programming: How to program OCaml in the new multicore-OCaml runtime. The OCaml multicore memory model
Deploying Apps
The Format Module
The Stdlib has the Format module for pretty printing. It’s a little tricky to get a hang of.
- Tutorial
- Blog post on Format
- Paper on Format
Tools
Functional Data Structures
- Purely Functional Data Structures: A classic book focusing on various data structures in the functional programming world. Can be very useful for understanding functional data structures, though OCaml obviously supports imperative data structures as well.
Writing Efficient Code
See Optimizing OCaml Performance.
Intermediate Level Books
- More OCaml: Algorithms, Methods, and Diversions by John Whitington: A meandering tour of functional programming with OCaml, introducing various language features and describing some classic algorithms. The book ends with a large worked example dealing with the production of PDF files. There are questions for each chapter together with worked answers and hints.
- Pearls of Functional Algorithm Design by Richard Bird:
Tackles 30 hard algorithm problems using function programming.
Although it is writtern for Haskell, trying to solve the problems in OCaml also helps develop functional programming.
- Partial solutions in OCaml are available here.
- Scientific Computing in OCaml and Owl: Covers OCaml and using the Owl library, which is a heavy-duty library for scientific and numerical computation as well as machine learning.
- Unix System Programming in OCaml by X. Leroy and D. Rémy (free): Introduction to Unix system programming, with an emphasis on communications between processes.
- OCaml for Scientists by Jon Harrop
Advanced Topics
Modules and Functors
Polymorphism
- OCaml polymorphism examples
- OCaml - polymorphic print and type erasure
- Weak Type Variables - when impurity breaks polymorphism
Existential Types
GADTs
- An excellent three part tutorial about GADTs, providing both explanations and examples from the OCaml ecosystem.
- GADTs in the OCaml manual
- Diff Lists: An application of GADTs to produce heterogenous lists.
- Detecting use-cases for GADTs
- Tradeoffs with GADTs
- Why GADTs matter for performance
- Cambridge University lecture: Programming with GADTs
- GADTs And State Machines: Excellent article about an interesting application of GADTs.
Phantom Types
Iterators
PPX (PreProcessor eXtensions)
See Metaprogramming for both libraries and articles.
FFI (Foreign Function Interface)
- See FFI.
Monads
- Excellent short introduction to monads
- Xavier Leroy’s Monadic Programming Lecture Slides (pdf) and code
- Cornell’s CS 3110 Lecture about monads, with code and recitation
- A small monads tutorial from the Monads library
- Discussion on what you can do with Monads and what are Monad Transformers
- Monadic Error Handling
- More Typeclasses in OCaml
Concurrency
Multicore
Type Theory
- Using, Understanding, and Unraveling the OCaml Language by Didier Remy: Covers both practical and type-theoretical aspects of OCaml.
- Types and Programming Languages by Benjamin C. Pierce: A friendly but serious book on type theory. Much of what’s covered is relevant to OCaml. OCaml implementations of typechecking are presented in the book.
- Advanced Topics in Types and Programming Languages: A more advanced anthology on type theory edited by Benjamin C. Pierce. Includes some articles that are relevant to OCaml.
Category Theory
Category Theory is a branch of abstract mathematics that discusses concepts that tend to pop up as patterns in functional programming.
- First steps with Category Theory and OCaml: Focuses on Monoids and Applicatives.
- Category Theory For Programmers: Not OCaml-specific, but a good book to learn Category Theory from.
- Category Theory For Programmers in OCaml: An ongoing work to translate CTFP’s code from Haskell to OCaml.
Academic Papers
See academic papers relevant to OCaml.
-
https://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/prolog/problemas/ ↩