Understanding F# Tuples
Here, in this article we are going to explore the fundamentals of F# tuples and show you how to create, access, and match patterns on them.
In F#, a tuple is an ordered sequence of elements of different types. Tuples are immutable, meaning that their values cannot be changed once they are created.
F# Tuples are a convenient way to group multiple values together, especially when those values have a natural relationship to each other.
For example, a tuple might represent a person’s name and age, or a point on a two-dimensional plane.
(Michael”, “Clarke”, 41) is an example of a 3-tuple consisting of two string names and an integer name. As you can see, it has the type (string string int).
Tuple could be two or more of the same or different type
Below are some examples to give you a better idea of what F# tuples are:
// Tuple of three integers. ( 1, 2, 5 ) // Pair of strings. ( "John", "Alexander" ) // Tuple of unknown types. ( x, y ) // Tuple that has mixed types. ( "Micheal", 16.0, 5) // Tuple of integer expressions. ( x / 2, y * 3)
F# Tuple Example
This is the basic example of creating a tuple for the user and printing its data:
The output will be:
Name: Ben, Age: 22, Is Employed: true
Three Numbers Average
The output of the above example is as follows:
Average of three numbers are: 4.433333
Accessing Individual Tuple Members
By using pattern matching, each member of the tuple could be assessed separately and then printed using the appropriate pattern.
Below is an example that illustrated the concept about the accessing of individual tuple members:
User Info: Micheal Clarke - Australia - 41
The first two items of a two-tuple are returned by the built-in function fst and snd which are available to the programmer.
Using the following example, we can illustrate the concept in more detail.
By using fst and snd we print out the first and second members of three F# tuples as follows:
First member: Sunflower Second member: Rose First member: Monica Second member: Ross First Name: Arsenal Second Name: Liverpool
Individual access using the let keyword to specify each tuple value into a different variable:
The output of the above code is:
Firstname is: Micheal Lastname is: Clarke Age is: 41 Australian Cricketer
This code is written in F# language and demonstrates tuple destructuring.
A tuple is a data structure that contains a fixed number of elements, each of which can have a different type. In this code, a tuple named “cricketer” is defined with four elements: “Micheal” as the first name, “Clarke” as the last name, 41 as the age, and “Australian Cricketer” as the country.
The next line of code uses tuple destructuring to extract individual values from the tuple and assign them to separate variables. The variables are declared using a comma-separated list, and their values are obtained from the tuple.
Finally, the values of the individual variables are printed to the console using the printfn function. The first and second printfn statements print the cricketer’s first and last name respectively. The third printfn statement prints the cricketer’s age, and the fourth printfn statement prints the country of the cricketer.
If you liked this article and found it informative regarding F# functional programming language, you can leave your feedback by reacting below.