Records
A record is a composite of other types of data, each of which is named. OCaml records are much like structs in C.
|
|
To build a value of a record type, we write a record expression, which looks like this:
|
|
To access a record and get a field from it, we use the dot notation that you would expect from many other languages. For example:
|
|
It’s also possible to use pattern matching to access record fields:
|
|
The n
, h
, and t
here are pattern variables. There is a syntactic sugar provided if you want to use the same name for both the field and a pattern variable:
|
|
Tuples
Like records, tuples are a composite of other types of data. But instead of naming the components, they are identified by position. Here are some examples of tuples:
|
|
A tuple with two components is called a pair. A tuple with three components is called a triple. Beyond that, we usually just use the word tuple instead of continuing a naming scheme based on numbers.