A tuple is a sequence of values. The values can be any type, and they are indexed by 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 kind 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 tuple of lists, In complex cases we can have combinations of all type in tuples. Fo example, 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 pair, so there are less method present for 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"
Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.