© Stefania Loredana Nita and Marius Mihailescu 2019
Stefania Loredana Nita and Marius MihailescuHaskell Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-4507-1_5

5. Tuples

Stefania Loredana Nita1  and Marius Mihailescu1
(1)
Bucharest, Romania
 

Sometimes in your applications you’ll need to group elements. In this chapter, you will learn how to do this using tuples and what the main predefined functions for tuples are.

Well, tuples are simple. They are a group of elements with different types. Tuples are immutable, which means they have a fixed number of elements. They are useful when you know in advance how many values you need to store. For example, you can use them when you want to store the dimensions of a rectangle or store the details of a student in the example from Chapter 4 (but it would be more difficult to read and follow the logic in the code).

Writing Tuples

Tuples are written between regular parentheses, and the elements are delimited by commas.
Prelude> ("first", "second", "third")
("first","second","third")
Prelude> :t ("first", "second", "third")
("first", "second", "third") :: ([Char], [Char], [Char])
Prelude> (1, "apple", pi, 7.2)
(1,"apple",3.141592653589793,7.2)
Prelude> :t (1, "apple", pi, 7.2)
(1, "apple", pi, 7.2)
  :: (Floating c, Fractional d, Num a) => (a, [Char], c, d)
Prelude> ("True", 2)
("True",2)
Prelude> :t ("True", 2)
("True", 2) :: Num b => ([Char], b)
Prelude> (True, 2)
(True,2)
Prelude> :t (True, 2)
(True, 2) :: Num b => (Bool, b)

In the first example, you have three elements. In the second example, you have four elements, and in the last two examples, you have two elements each. Note that "True" is different from True. The first one, in quotation marks, is a String (or [Char]) value, while the second one is a Bool value.

You call a tuple with two elements a pair and a tuple with three elements a triple.

Actually, a tuple with more than three elements is not so common. Tuples are useful when you need to return more values from a function. In Haskell, when you want to return more values from a function, you need to wrap them in a single data structure with a single purpose, namely, a tuple.

Note that a tuple can have another tuple as an element, as shown here:
Prelude> (5, 'a', (2.3, False, "abc", 4))
(5,'a',(2.3,False,"abc",4))
Prelude> :t (5, 'a', (2.3, False, "abc", 4))
(5, 'a', (2.3, False, "abc", 4))
  :: (Fractional a1, Num a2, Num d) =>
     (a2, Char, (a1, Bool, [Char], d))

Predefined Functions for Pairs

You have seen that a particular type of tuple is a pair. Pairs are more widely used than other tuples. Let’s suppose you want to declare a point in the Cartesian space, giving the x-coordinate and y-coordinate.
Prelude> (3.2, 5.7)
(3.2,5.7)
Prelude> let point = (3.2, 5.7)
If you need the x-coordinate of point, Haskell has a predefined function for you.
Prelude> fst point
3.2
The fst function returns the first value in the pair. Similarly, the snd function returns the second value in the pair.
Prelude> snd point
5.7
Another predefined function is swap. It belongs to the Data.Tuple module, and to use it, you first need to import this module. Then, you need to declare again the point variable, because it does not exist in the new scope. Finally, call the swap function.
Prelude> import Data.Tuple
Prelude Data.Tuple> let point = (3.2, 5.7)
Prelude Data.Tuple> swap point
(5.7,3.2)

The result of swap is a pair with switched elements. For the moment, don’t worry about modules and importing. You will learn more about modules in Chapter 7. Note that Data.Tuple contains two more functions: curry and uncurry (you will learn how they work in Chapter 7).

Using the predefined functions over pairs, let’s compute the distance between two points, A and B, in the Cartesian space. The distance is given by the following formula:
$$ d=sqrt{{left({x}_A-{x}_B
ight)}^2+{left({y}_A-{y}_B
ight)}^2} $$
This is easy, because you already have a function that computes the square root of a number (sqrt) and a function that raises a number to another number (the power sign, ^). Therefore, the distance would look like this:
Prelude> let pointA = (2.4, 6)
Prelude> let pointB = (-7, 3.5)
Prelude> d = sqrt ((fst pointA - fst pointB)^2 + (snd pointA - snd pointB)^2)
Prelude> d
9.726767191621274

Summary

In this chapter, you learned what tuples are and when they are useful. In addition, you worked with predefined functions over tuples.

Also in this chapter, you looked at modules and saw a preview of how they can be imported.

References

  1. 1.

    C. Hall and J. O’Donnell, “Introduction to Haskell” in Discrete Mathematics Using a Computer, pp. 1–33 (Springer, 2000)

     
  2. 2.

    A. S. Mena, Beginning Haskell: A Project-Based Approach (Apress, 2014)

     
  3. 3.
     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.206.116