Want to keep learning?

This content is taken from the The University of Glasgow's online course, Functional Programming in Haskell: Supercharge Your Coding. Join the course to learn more.

Skip to 0 minutes and 4 seconds 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,

Skip to 0 minutes and 27 seconds 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.

Skip to 0 minutes and 38 seconds In Haskell syntax, you express a pair like this:

Skip to 0 minutes and 44 seconds 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.

Skip to 1 minute and 30 seconds 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.

Skip to 2 minutes and 17 seconds 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!

Introducing Pairs in Haskell

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?

Share this video:

This video is from the free online course:

Functional Programming in Haskell: Supercharge Your Coding

The University of Glasgow

Get a taste of this course

Find out what this course is like by previewing some of the course steps before you join: