Functional Programming - Tuple



A tuple is a compound data type having a fixed number of terms. Each term in a tuple is known as an element. The number of elements is the size of the tuple.

Program to define a tuple in C#

The following program shows how to define a tuple of four terms and print them using C#, which is an object-oriented programming language.

using System; 
public class Test { 
   public static void Main() { 
      var t1 = Tuple.Create(1, 2, 3, new Tuple<int, int>(4, 5));   
      Console.WriteLine("Tuple:" + t1);    
   } 
} 

It will produce the following output −

Tuple :(1, 2, 3, (4, 5)) 

Program to define a tuple in Erlang

The following program shows how to define a tuple of four terms and print them using Erlang, which is a functional programming language.

-module(helloworld).  
-export([start/0]).   

start() -> 
   P = {1,2,3,{4,5}} ,  
   io:fwrite("~w",[P]). 

It will produce the following output −

{1, 2, 3, {4, 5}} 

Advantages of Tuple

Tuples offer the following advantages −

  • Tuples are fined size in nature i.e. we can’t add/delete elements to/from a tuple.

  • We can search any element in a tuple.

  • Tuples are faster than lists, because they have a constant set of values.

  • Tuples can be used as dictionary keys, because they contain immutable values like strings, numbers, etc.

Tuples vs Lists

Tuple List
Tuples are immutable, i.e., we can't update its data. List are mutable, i.e., we can update its data.
Elements in a tuple can be different type. All elements in a list is of same type.
Tuples are denoted by round parenthesis around the elements. Lists are denoted by square brackets around the elements.

Operations on Tuples

In this section, we will discuss a few operations that can be performed on a tuple.

Check whether an inserted value is a Tuple or not

The method is_tuple(tuplevalues) is used to determine whether an inserted value is a tuple or not. It returns true when an inserted value is a tuple, else it returns false. For example,

-module(helloworld).  
-export([start/0]).  

start() ->  
   K = {abc,50,pqr,60,{xyz,75}} , io:fwrite("~w",[is_tuple(K)]). 

It will produce the following output −

True

Converting a List to a Tuple

The method list_to_tuple(listvalues) converts a list to a tuple. For example,

-module(helloworld).  
-export([start/0]).  

start() ->  
   io:fwrite("~w",[list_to_tuple([1,2,3,4,5])]). 

It will produce the following output −

{1, 2, 3, 4, 5} 

Converting a Tuple to a List

The method tuple_to_list(tuplevalues) converts a specified tuple to list format. For example,

-module(helloworld).  
-export([start/0]).  

start() ->  
   io:fwrite("~w",[tuple_to_list({1,2,3,4,5})]). 

It will produce the following output −

[1, 2, 3, 4, 5] 

Check tuple size

The method tuple_size(tuplename) returns the size of a tuple. For example,

-module(helloworld).  
-export([start/0]).  

start() ->  
   K = {abc,50,pqr,60,{xyz,75}} ,  
   io:fwrite("~w",[tuple_size(K)]). 

It will produce the following output −

5
Advertisements