Instructor: To create a tuple, we open a parenthesis. Add two or more values and close it with a parenthesis again. The result is an immutable tuple of two elements. A tuple can hold fixed number of values. These can be of same or different types. In addition, each element is identified by position, rather than by name.
Let's create another one with an integer, two Booleans, and a string. As you can see, you can combine all sorts of types in there. That's the one law. This time, we're going to nest a tuple. We describe a circle with a point having X and Y position and the radius. It doesn't indicate that it is a circle, but we can create a tuple type and provide a type annotation for clarity.
Let's see how we can access elements of a tuple. The standard library provides two convenience functions to access the first two elements. FST for first, to access the first item, and SND for second, to access the second one. In case you want to access any other element, we are required to use the structuring.
So far, we have seen structuring only for records, but it also works for tuples using parentheses instead of curly braces. Value of three is not bound to the name C. In case you don't want all positions to be assigned, we can replace the ones to be ignored with an underscore.
Keep in mind the order within tuples is fixed. Every tuple is immutable. This means we don't update tuple, we'd rather create a new one. For example, like this. You created another circle with the second element, the radius, and you replaced the six.
In general, it's recommended to keep the usage of tuples local, update the structures that are long-living and passed around often. It's recommended to prefer records.
Next, we want to focus on the typelists. Lists are homogeneous and immutable. Homogeneous means we can't have different types of items in a list. That said, we can't have a variance storing different types and create a list of constructors.
Second trait of lists is that they're immutable. This means we can't append, replace, or prepend an item to the same list. We'd rather have to create a new one. Then, we'll do ways how to add new elements by creating a new list.
For once, we use the append function on the lists module shipping with the standard library. The other way is to use Reason special operator to concatenate lists. The third one is using the spread operator. Keep in mind this only with prepending one or multiple elements, but not for appending elements.
The recommended way to access a list is using the switch expression. We will cover this later extensively in the pattern matching lesson. To give you a glimpse of it, here's an example where we get out the first element of the list and print it as a string.
As an alternative to access a list item, we can use list.end. Keep in mind, it will raise an exception in case the list is too short, and therefore it's rather recommended to rely on other strategies like switch expressions.
To create a new list and manipulate each item, we have list.map at our disposal. It accepts a function as the first parameter and the regional list as a second one. In the standard library, there are many more utility functions like find, filter, sort, exist, split and so on. We can use the API recommendation on the Reason website to figure out more.
Let's move on to arrays. Arrays are declared the same way as lists, except the array's content is wrapped in pipes and brackets. Compared to lists, one significant difference is that array's immutable.
You can access an item using brackets, but also assign a new value to this position using the equal sign. Same as of list, the standard library ships a couple of useful utility functions. For example, array.map.