Programming Languages Section

OCaml, the Safe Language

Card image
Post by Amina Delali, January 03rd, 2022

Some Facts

OCaml is a multi-paradigm general purpose programming language. So you can write code in imperative and object oriented style, as well as in functional programming style. So, it is not a pure functional programming language as Haskell is, but it is fast, flexible, expressive, and safe. It is mostly used, when safety matters, and when error occurrences are critical. It is strongly typed and supports type inference. Besides that, applications written in OCaml can be deployed in one executable file. It can be used for embedded devices, operating systems, as well as for finance applications. So the language is used by Jane Street, Agora, and Crowdstrike, among other companies.



How to install it

  • on Windows:

    You can either install an experimental Windows version using the graphical installer available from this installation page, or you can install it on the Windows Subsystem for Linux, following these instructions:

    1. install bash if it is not installed yet. To do that, you can open Powershell and run the following command: wsl --install You must have the Windows 10 version 2004 or higher. If it is not the case, follow the instructions available in this page. For more information about the installation of bash on Windows, you can check this link.
    2. Open bash, by writing bash on the start menu and selecting 'Bash on Ubuntu on Windows'.
    3. run the following command to add OCaml repository: sudo add-apt-repository ppa:avsm/ppa
    4. run sudo ap update
    5. run sudo apt install ocaml ocaml-native-compilers camlp4-extra opam to install OCaml and its package manager OPAM .
    6. run the command: sudo apt install make
    7. initialize OPAM by this command: opam init ( you may also have to type in the ctrl-c command and run OPAM init again)
    8. open the ~/.bashrc file and add the following line at the end of the file: eval `opam config env` Then close the bash shell and open it again, to let the changes take effect.
    9. To verify the installation of OCaml, just run it: ocaml
    10. install necessary packages: opam install ocamlfind
      opam install ounit
      sudo apt install rlwrap
    11. For explicit details about the OCaml installation, you can refer to this step by step guide.
  • on ubuntu :

    Just run the following commands:

    1. sudo apt update

    2. sudo apt install ocaml
    3. to check the installed version, run this ocaml --version
    4. If you want to install OPAM, you can run the following commands: sudo apt update
      sudo apt install opam

The Hello World Example

    1. first create a file named hello.ml in the home directory.
    2. write and save the following code in the hello.ml file:
      print_string("Hello World\n")
    3. to compile the file, open the terminal from the home folder and write this: ocamlc -o hello hello.ml
    4. then run the generated binary by this command: ./hello
    5. For more options you can check this page.


Additional Information

For more information about the OCaml language and the corresponding code, you can check the following pages:

Something to say ?

If you want to add something about the OCaml language or about this post, please feel free to do it by commenting below 🙂 .