Skip main navigation

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.
4
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,
26.9
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.
38.2
In Haskell syntax, you express a pair like this:
44
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.
90
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.
137.3
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:

(1,2)

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

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education