Chapter 8 : Tuples

Tuple is a composite type that gathers multiple values in a specific order which can be retrieved with their indexes.

Defining Tuples

To define a tuple, use types separated by the * operator :

type name = string * string  // Alias

And to define a value of this type :

let pilot : name = ("Garry", "Topper") // Optional parentheses

Destructuring Tuples

If we want to get the first and last name of the full_name type, we can use destructuring. Destructuring a tuple allows you to give names to the elements inside the tuple.

let (first_name, last_name) : full_name = pilotlet login : string = String.sub 0n 1n first_name ^ "_" ^ last_name

We use the underscore _ to indicate that we ignore an element of the tuple.

let first_name ((first_name, _): full_name) = first_namelet alice = first_name full_name


You can access each component of a tuple by their position :

let my_name_first_name : string = my_name.0let my_name_last_name : string = my_name.1

Your mission

1- Create a tuple type coordinates representing a 3D location.

2- Define earth_coordinates at coordinates 2,7,1.

3- Let’s say you made a mistake in the definition. Define a new constant modifiedearthcoordinates which reuses parameters of earth_coordinates except for the last parameter of earth_coordinates which is fixed to 5. Direct access by postion is asked (do not destructure earth_coordinates)