OCAML Course

Composite types

Go back

A composite type, also called tuple, or record, is a type made of many types. You could see that as a vector in mathematics. This is useful because a function may only return one value, so you can wrap many values inside a tuple using this. You will also use this when creating new types.

let person = ("name", 5);;

You can create a composite type, also called tuple (or record). When the type is made of two types, we are calling it a couple.

let person = ("name", 5)
(* same, but that's not readable 🙄 *)
let person = "name", 5

The type is string * int.


Extract values from a couple

When the tuple is made of two types, we are calling it a couple. You got the functions fst (first) and snd (second), to extract its values.

let name = fst person (* "name" *)
let age = snd person (* 5 *)

Extract values from a tuple

let (name, age) = person

let (name, _) = person (* name only *)
let (_, age) = person (* age only *)