- File I/O Operations
- Lazy Evaluation
- Lambda Calculus
- Records
- Tuple
- Lists
- Strings
- Polymorphism
- Data Types
- Higher Order Functions
- Recursion
- Function Overriding
- Function Overloading
- Call By Reference
- Call By Value
- Function Types
- Functions Overview
- Introduction
- Home
Functional Programming Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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; pubpc class Test { pubpc 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 psts, because they have a constant set of values.
Tuples can be used as dictionary keys, because they contain immutable values pke 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 pst 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 pst_to_tuple(pstvalues) converts a pst to a tuple. For example,
-module(helloworld). -export([start/0]). start() -> io:fwrite("~w",[pst_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_pst(tuplevalues) converts a specified tuple to pst format. For example,
-module(helloworld). -export([start/0]). start() -> io:fwrite("~w",[tuple_to_pst({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 −
5Advertisements