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"
Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know.
Knowledge is the only thing that doubles when you spend it.
I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation