Printing hello world in OCaml

Reading time ~2 minutes

Why writing a tutorial ?

There are indeed plenty of good resources if one is willing to learn OCaml (OCaml tutorials may be a good start). However, the usual road one follows when starting a new language, like printing hello world is not commonly presented. This might be due to the fact that printing hello world is actually one of the least functional thing to do…

Besides, OCaml comes with a utop toplevel, different compilers: by default ocamlc and ocamlopt but plenty of others are available… Knowing where to start may not be obvious.

Here we will compile and execute a program that prints hello world.

Setting up ocaml

The best starting point is to install opam which is to OCaml what pip is to Python. opam also enables to switch between various compilers,

Installing OPAM details the different steps depending on your distribution. Assuming you are using Ubuntu, the following should do the job:

sudo apt-get install opam

Once opam is installed, the following commands will enable you to use OCaml

# environment setup
opam init
eval `opam env`
# install given version of the compiler
opam switch create 4.08.0
eval `opam env`

# check you got what you want
which ocaml
ocaml -version

In my case:

~$ ocaml -version
The OCaml toplevel, version 4.08.0

The project

The usual extension of an OCaml file is .ml. Let’s just create a dedicated folder (called hellocaml below), in which we place a main.ml file. The contents of the file main.ml will be pretty simple:

let () =
  print_string "Hello world\n";

print_string simply prints the input string in the terminal. Note that this print function is strongly typed, you cannot write:

print_string 42;

Instead, one would have to write:

print_int 42;

Note that the \n has nothing magical in it. By default, language such as python will jump to the next line once executed. In OCaml, the print_string function does not do this by default. The following code would produce the same output.

let () =
  print_string "Hello ";
  print_string "world\n";

For now the contents of our directory looks like:

hellocaml/
└── main.ml

Executing the project

user@:~/hellocaml$ ocaml main.ml 
hello world

Compiling the project

Ocaml has different build systems but we will not use any of them for now. We will simply use the default tools to turn our code into an executable.

ocamlopt and ocamlc

For now, you do not need to know a lot about these two.

Compiling the project

Typing the following command:

ocamlc main.ml

The directory now contains:

hellocaml/
├── a.out
├── main.cmi
├── main.cmo
└── main.ml

Executing the project

user@:~/hellocaml$ ./a.out 
hello world

What is next ?

All the cool features of the language ! Nothing about the functional aspect has been discussed

Resources

Online

Why OCaml, video. If you wonder about the points of functionnal programming, this video summarizes key aspects of OCaml.

99 problems in OCaml if you prefer to learn with exercises instead of tutorials ;)

Stay tuned, I plan to write more detailed tutorials!

Books

Opam permissions on Manjaro

bwrap: No permissions to creating new namespaceWhen installing ounit, I faced the following issue:$ opam install ounit[NOTE] It seems you...… Continue reading

Winning at motus

Published on August 11, 2019

OCaml functor example : a progress bar

Published on June 15, 2019