Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £35.99 £24.99. New subscribers only T&Cs apply

Find out more

Introducing Pairs in Haskell

Pairs are composite data structures in the Haskell programming language. In this video, Jeremy Singer explores how to build and access pairs.
Look … here is a pair. No, not a PEAR pear but a pair PAIR, as in two items, two related items of fruit, an apple and a banana. Ordering is important in pairs. First I will eat the apple,
and then, after that, I’ll eat my banana So, two related items with a set ordering. Now let’s think about pairs in Haskell.
In Haskell syntax, you express a pair like this:
two parentheses, a comma in between the two elements so for instance ( 1 , 2 ) is an Integer pair. it’s a composite value with two individual items inside. Haskell has built-in accessor functions for pairs as well. If I say first (spelled fst) 1 comma 2 then that will return the first element from this pair which is 1 and if I say second (spelled) snd 1 2 then that will return 2 which is the second element in this composite value here.
Pairs don’t necessarily have to have elements of the same type inside them, so for instance, I might say something like 5 (which is an Integer) comma, string quotes gold rings and there the first element is an Integer, the second element is a String or character list, as you know. So this pair has two elements of different types inside it. I can be more general still, so it might be that I decide to have more than two elements, I could have a triple, and then I would have three individual elements, with commas separating them, and the brackets outside. Or a quadruple, or a quintuple. In the general case we call the thing a tuple which has n items inside it.
Pairs are not to be confused with lists. You have already seen lists in some previous example videos and sessions. So what I would like you to do is to think about concrete differences between pairs and lists , and maybe comment on those differences in the comments section below. But for now, keep on coding!

Pairs, like lists, are a technique for building composite data out of individual values. A pair of Integers can be written as:


and a pair of Strings might be:

("apple", "banana")

Notice that pairs can also have elements with distinct types, for example:

(10, "green bottles")

A tuple is a generalization of a pair, since it has an arbitrary number of elements. In the comments below, please explore some differences between tuples and lists. Can you think of scenarios where one data structure would be more appropriate than the other?

This article is from the free online

Functional Programming in Haskell: Supercharge Your Coding

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now