Tuples

A pair is one value

A tuple groups values in a fixed number of positions, written in parentheses and separated by commas. A pair is a tuple of two, and its type names both components:

pair :: (Int, String) pair = (1, "one")

The type is (Int, String), and it is not the same as (String, Int): the order is part of the type, and the compiler will not let the two be swapped. The value (1, "one") is one value, not two, which is why a function can return it.

Two functions read the halves of a pair:

module Main (main) where pair :: (Int, String) pair = (1, "one") main :: IO () main = do print (fst pair) print (snd pair)

1
"one"

fst and snd work on pairs only; a tuple of three needs a pattern instead. And the names are less useful than they look: in (Int, String) they say nothing about which half is the count and which the label, which is the problem records solve in a later lesson.

A program with pairs

This program uses tuples in three ways: as a value, pair; as the result of a function that has to give back two numbers, minMax; and in the lists that zip and unzip build.

module Main (main) where minMax :: [Int] -> (Int, Int) minMax [] = error "minMax: empty list" minMax (x : xs) = foldr step (x, x) xs where step y (lo, hi) = (min y lo, max y hi) pair :: (Int, String) pair = (1, "one") main :: IO () main = do print pair print (fst pair) print (snd pair) print (minMax [3, 1, 4, 1, 5]) print (zip [1, 2, 3 :: Int] "abc") print (unzip ([(1, 'a'), (2, 'b')] :: [(Int, Char)]))

ghc -Wall -O1 -o tuples Tuples.hs
./tuples

[1 of 2] Compiling Main ( Tuples.hs, Tuples.o )
[2 of 2] Linking tuples
(1,"one")
1
"one"
(1,5)
[(1,'a'),(2,'b'),(3,'c')]
([1,2],"ab")

The fourth line is the interesting one: minMax [3, 1, 4, 1, 5] is (1, 5), the smallest and the largest of the list, from one call. The signature says so before the body is read: [Int] -> (Int, Int) takes a list and returns a pair.

zip and unzip

zip takes two lists and gives a list of pairs, one element from each at a time. It stops at the shorter list, so an extra element is dropped without an error:

main :: IO () main = print (zip [1, 2, 3 :: Int] "abc")

[(1,'a'),(2,'b'),(3,'c')]

The first list has numbers and the second characters, and the result is a list of (Int, Char). zip is the standard way to walk two lists together, and with a function in between it becomes zipWith: zipWith (+) [1, 2] [10, 20] is [11, 22].

unzip goes the other way and takes a list of pairs to a pair of lists:

main :: IO () main = print (unzip ([(1, 'a'), (2, 'b')] :: [(Int, Char)]))

([1,2],"ab")

The annotation matters. Without it, unzip does not know whether the character of each pair is a character or something else, and print leaves the decision open; with it, the result is a list of numbers and a string.

A function that returns two values

A function has one result type, so returning two things means returning one pair. minMax is written that way, and the empty case has to be decided: there is no smallest element of an empty list, so the definition calls error with a message. That is a deliberate crash, and the lesson on exceptions shows the alternative, a Maybe.

The recursive step receives the pair as a pattern, step y (lo, hi), which is how a tuple is taken apart without fst and snd: the pattern names both halves at once. Patterns on tuples are the subject of a later lesson; here one example is enough.

How many components

GHC has a tuple type for every size up to a large fixed limit, and (1, 2, 3) has type (Int, Int, Int). But only pairs have fst and snd, and the type of a six-component tuple says less than its length.

The practical rule is short: a tuple of two or three is fine as an internal detail, such as the result of minMax or the pair from zip. When a value has four or more parts, or when the same tuple travels through several functions, give it a name with data. The lesson on product types shows the difference in one file, and the one on records adds the field names.

Exercises

  1. Write swap :: (a, b) -> (b, a) and print swap (1, "one"). Does the signature allow a swap of a pair with different types?
  2. Use zip and map to add the corresponding elements of [1, 2, 3] and [10, 20, 30]. Then do the same with zipWith and compare the two expressions.
  3. Call minMax [] and run the program. What is printed, and what is the exit status?
  4. Take the pair (1, "one") apart with a pattern in a let: let (n, word) = pair. Print n and word.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which functions read the two halves of a pair?

    Answer

    fst and snd

    fst returns the first component of a pair and snd the second; head and tail work on lists.

  2. What is the type of (1, "one")?

    Answer

    (Int, String)

    The parentheses with a comma make a tuple, and the order of the component types is part of its type.

  3. What does zip [1, 2, 3] "ab" give?

    Answer

    [(1, 'a'), (2, 'b')]

    zip stops at the shorter list, so the third number is dropped without an error.

  4. Why does fst (1, 2, 3) not compile?

    Answer

    Because fst is defined for pairs only

    fst has the type (a, b) -> a, and a three-component tuple is not a pair; take it apart with a pattern instead.

  5. What is unzip [(1, 'a'), (2, 'b')]?

    Answer

    ([1, 2], "ab")

    unzip turns a list of pairs into a pair of lists, one list per component.

  6. What does this program print? module Main (main) where main :: IO () main = print (fst (1, "one"))

    Answer

    1

    fst returns the first component, which is the number 1.

  7. What does this program print? module Main (main) where main :: IO () main = print (1, "one")

    Answer

    (1,"one")

    The parentheses make one tuple, and print shows it with the quotes that rebuild the string.

  8. What does this program print? module Main (main) where main :: IO () main = print (zip [1, 2 :: Int] [True, False])

    Answer

    [(1,True),(2,False)]

    zip pairs the elements by position, so each number comes with the boolean at the same index.

  9. A function must return the smallest and the largest value of a list. What is the Haskell way?

    Answer

    Return a pair (smallest, largest)

    A function has one result, and a pair is one value holding two, so the signature [Int] -> (Int, Int) says it exactly.

  10. When should a tuple of four components become a data type?

    Answer

    When the value is passed between functions or its components need names

    A data type gives the value a name and each component a field name, so a reader does not have to count positions.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image