A tuple is a sequence of values. The values can be of any type, and they are indexed by an integer, so tuples are not like lists. Tuples are immutable which means you cannot add more elements to the tuple as the program runs.
Tuples are marked by parentheses with elements delimited by commas. Let's look at some sample tuples:
(True, 1, False) ("Hello Mars", False) (9, 5, "Two", False, 'b')
The first example is a tuple containing two elements: True and 1 which are Boolean and number respectively. The next example again has two elements: "Hello Mars"(String) and False (Boolean).
The third example is a tuple consisting of five elements: 9 (a number), 5 (another number), "Two" (a string), False (a boolean value), and 'b' (a character). From this itself you can conclude that tuple accepts all kinds of type in a single tuple.
Tuples are useful when you want to return more than one value from a function. In many languages, returning two or more things at once often requires wrapping them up in a single-purpose data structure, maybe one that only gets used in that function. In Haskell, we would return such results as a tuple.
We can store tuples inside other tuples just like normal elements. We can also have a tuple of lists, In complex cases, we can have combinations of all type in tuples. Fo example, a tuple containing a Tuple, List, Boolean, number so on.
((2,3), False) ((4,3), [2,6], True, 9) // tuple with different kinds [(7,2), (1,4), (9,2)] //list of tuples [1,6], [2,8], [3,6]] // tuple of lists
Most of the time the Tuple are in pairs, so there is less method present for the tuple.
We can retrieve the first and second values using the
Prelude> fst (2, 5) 2 Prelude> fst (True, "boo") True Prelude> snd (5, "Hello") "Hello"
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.