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.

Furthermore, we are going to discuss some of the common use cases for F# tuples, such as expressing complex data structures and returning multiple values from a function.



F# Tuples

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:

Example: 

let user (x, y , z) = printfn "Name: %s, \nAge: %d,\nIs Employed: %b" x y z let usr = user("Ben", 22, true)The output will be: Name: Ben, Age: 22, Is Employed: true

The output will be:

Name: Ben,
Age: 22,
Is Employed: true

Three Numbers Average

In the example below, you will find an average function that takes a tuple of three values as input:

Example: 

let calAverage (m, r, x) = let sum = m + r + x sum / 3.0let avg:float = calAverage (7.1, 2.8, 3.4) printfn "Average of three numbers are: %f" avg

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:

Example: 

let display tuple = match tuple with | (m, r, x) -> printfn "User Info: \n%A – %A – %A" m r xdisplay ("Micheal Clarke", "Australia", 41 )
This is what we will get as an output:
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.

Example: 

let tuple1 = ("Sunflower", "Rose") printfn "First member: %A" (fst tuple1) printfn "Second member: %A" (snd tuple1)let tuple2 = ("Monica", "Ross") printfn "\nFirst member: %A" (fst tuple2) printfn "Second member: %A" (snd tuple2)let tuple3 = ("Arsenal", "Liverpool") printfn "\nFirst Name: %A" (fst tuple3) printfn "Second Name: %A" (snd tuple3)

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:

Example: 

let cricketer = ("Micheal", "Clarke", 41, "Australian Cricketer")let firstname, lastname, age, country = cricketerprintfn "Firstname is: %s" firstname printfn "Lastname is: %s" lastname printfn "Age is: %d" age printfn "%s" country

The output of the above code is:

Firstname is: Micheal
Lastname is: Clarke
Age is: 41
Australian Cricketer

Example Explanation

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.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *