OCaml Tutorial for Beginners [Updated 2021]
In this tutorial, we are talking about Latest Programming Language OCaml.
OCaml - It is a lightweight functional language fueled by six basic data types powerful custom data
types powerful custom data type system recursion and pattern matching aggressively modular
safety first language
- Objective CAML.
- Supports functional, imperative, and object-oriented styles.
- includes an interactive top-level interpreter a bytecode compiler and an optimizing native-code compiler.
- It has a large standard library.
- free and open-source.
How to Installing OCAML on Ubuntu Linux and Windows or Mac?
We are installing OCAML using OPAM on Ubuntu Linux and we are also installing third-party Ocaml libraries.
Windows Command -
- $ ocaml
- OCaml version 4.11.1
- $ opam install utop
Linux Command-
- # Homebrew brew install opam
- # environment setup opam init eval `opam env`
- $ which ocaml /Users/frank/.opam/4.11.1/bin/ocaml
Mac Command-
- # MacPort
- port install opam
- # install given version of the compiler opam switch create 4.11.1 eval `opam env`
- $ ocaml -version
- The OCaml toplevel, version 4.11.1
OCaml used to develop applications and it is also interpreters language. If you are developing software and mobile application then Ocaml is playing an important role.
- Strong static type scope
- Hybrid Vigor
- Efficient Implimentation
Let's discuss some Ocaml Data types
| DataTypes | Example-Values |
|---|---|
| int | 63-bit signed int on 64-bit processors, or 31-bit signed int on 32-bit processors |
| bool | IEEE double-precision floating point, equivalent to C's double |
| float | A boolean, written either 'true' or 'false' |
| String | An 8-bit character |
| Char | sequence of 8 bit chars |
How to write an Ocaml if-else conditions and loops
| if boolean-condition then expression else other-expression |
| for variable = start_value to end_value do expression done |
| Mutually recursive functions # let rec even n = match n with | 0 -> true | x -> odd (x - 1);; Error: Unbound value odd |
| Dealing with errors # exception E;; exception E # exception E2 of string;; exception E2 of string |
| Pattern matching
# let rec factorial n =
if n <= 1 then 1 else n * factorial (n - 1);;
val factorial : int -> int = |
| Lists # [];; - : 'a list = [] # [1; 2; 3];; - : int list = [1; 2; 3] # [false; false; true];; - : bool list = [false; false; true] # [[1; 2]; [3; 4]; [5; 6]];; - : int list list = [[1; 2]; [3; 4]; [5; 6]] |
how to define pointer in ocaml?
| /* Cells and lists type in C */ struct cell { int hd; struct cell *tl; }; typedef struct cell cell, *list; |
| # type 'a pointer = Null | Pointer of 'a ref;; type 'a pointer = Null | Pointer of 'a ref |
| # let ( !^ ) = function
| Null -> invalid_arg "Attempt to dereference the null pointer"
| Pointer r -> !r;;
val ( !^ ) : 'a pointer -> 'a = |
How to define a ocaml function
| module F (X : X_type) = struct ... end |
| module F (X : X_type) : Y_type = struct ... end |
| module F (X : X_type) : Y_type |
How to define a object of OCaml
| # class stack_of_ints = object (self) Val mutable the_list = ([] : int list) method push x = the_list <- -="" ::="" :="" end="" in="" int="" list.tl="" list="" method="" mutable="" object="" peek="" pop="" push="" result="" size="List.length" stack_of_ints="" the_list="" val="" x=""> unit method size: int end-> |
In an OCaml, there are two types of techniques to help to find out the bugs.
- Tracing
- OCaml debugger
Why we are used BuckleScript with OCaml?
- BukleScript is a backend for the Ocaml Compiler which emits Javascript.
- Built by Bloomberg engineers.
- The purpose was to leverage the OCaml advantage that was being used for a smaller project in large javascript projects.
- Essentially is mapping one OCaml module to one javascript module and calling it when invoked.
- OCaml is a safe and fast functional programming language
- let's use that to our advantage with BuckleScript!
- Functional Programming accessible to javascript developers
- Best of both world

Comments