New to OCaml? We recommend checking out A Tour of
OCaml to get up to speed.
Local development
To run the code for your challenge locally, you’ll need opam and dune installed. Our test runners use Dune v3.16 (as of September 2024). The script for your challenge (./your_program.sh
) will run your code using these tools. It will look something like this:
File structure
- The files for your solution are placed in the
src/
directory. src/main.ml
is the entry point for your OCaml program.
Adding more files
You can organize your code by adding more.ml
files in the src/
directory. The test runner will include them when building and running your code.
For example, if you add a file at src/foo.ml
, you can reference it in main.ml
like this:
Adding dependencies
If you’re looking for OCaml libraries, you can browse the opam package repository.1
Specify dependencies
You need to specify dependencies in both the Then, update the
dune-project
and dune
files.For example, to add the yojson
library for handling JSON, first update dune-project
as follows:dune
file to include yojson
as a library:For more details, check the OCaml documentation on Adding a Dependency.
2
Install dependencies
You need to regenerate the Then, run this command to install the dependencies:
.opam
file from dune-project
before installing dependencies.First, run this command to update the .opam
file:If you’ve already used the library in an
.ml
file, dune build
will exit with an error. This is okay — it’ll still update the .opam
file (required for the next step).3
Use dependencies
Here’s an example of how to use the
yojson
library: