16 Records and Tuples

Records allow us to aggregate data together.

We can define a record type:

We can then create values of the record type

Fun fact! If we want to include code we've written in a file in utop via the use directive: #use "filename.ml".

We can access a field in a record with the . operator


Another basic kind of data type is a tuple. They're like records; however, they're unnamed.

This is a tuple with 3 values. We can even give this tuple type a name.

These two values ([6] and [7]) are the same.

Say we wanted to access the elements of this tuple. OCaml defines the fst and snd methods that get the first and second items from a tuple respectively.

It's important to note that these only work on pairs. OCaml will throw an error if we don't have a pair