Skip main navigation

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

Find out more

Zip that List

Multiple lists may be combined with the zip function. In this video, Dr Jeremy Singer explains how zip works.
5
JEREMY: Hello everyone. Time for some more functional programming. Let’s go.
12.9
You know, the zip is a remarkable invention. The two rows of interlocking teeth are combined using the zipper. Well today, we’re going to think about the Haskell zip function, which combines not interlocking teeth, but distinct lists of elements.
40.6
Here, I’m drawing a list of strings, different food stuffs here– fish, bread, and salt– represented as Haskell strings. Second list– chips, butter, and pepper. And what we see is that the first two elements from each list really belong together as a pair– fish and chips.
61.1
It’s the same with the second two elements from the lists. Bread and butter belong together as a pair.
69.6
And indeed the last element from each list, salt and pepper, they also belong together as a pair. So from a pair of lists, I now have a list of pairs, which is precisely the behavior of the zip function.
87.8
Hi. Now, we’re going to evaluate some zip function calls inside the Haskell interpreter, GHCi.
100.1
If you haven’t installed GHCi on your system yet, don’t worry. You can evaluate all the code I’m going to show you using the TryHaskell online environment, which you should have used for your tutorials. First, let’s zip together two lists of integers. We’ve got a list containing elements 1, 2, 3, and another list containing elements 4, 5, 6. And when I zip these two lists together, I’ll end up with a new list of pairs of integers. The first element from the first list is in a pair with the first element from the second list and so on.
144.1
I can zip together two lists of different types. There is an integer list. And here is a string, which, as you know, in Haskell is represented as a character list. So this should give me a list of pairs where the first element of each pair is an integer and the second element of each pair is a character. Indeed, 1 and a pair together. 2 and b are paired together. And 3 and c are paired together.
172.9
You might be asking, what if I want to zip together three lists to get a single list of triples? And that too is possible. Let’s use the zip3 function. So there is a character list, string Glasgow. String Beijing is another character list. And the string Nairobi is another character list. And indeed, when I evaluate this zip3 function, I’m going to end up with a single list of triples. The first triple has the first letter of each of those cities. And the last triple has the last letter of each of those triples and so on.
214.4
Conveniently, the strings Glasgow, Beijing, and Nairobi are all of seven characters in length. What happens if I try to zip together lists that have different number of elements? So here’s the list of integers from 1 to 10 inclusive, which has length 10. And here is the list of lowercase letters from A to Z inclusive, which has length 26. I’m going to try and zip together the list of integers 1 to 10 and the list of characters A to Z. Look, the output is as long as the shorter of the two inputs.
263.1
So I’ve produced a list of 10 pairs and used up all the input from the integers 1 to 10, but only the first elements from the alphabet A to Z. So the length of the output is the same as the length of the shorter of the two inputs.
285.7
Now, let’s think about the zipWith function, which is a generalization of zip. Whereas zip can only combine elements from two input lists by creating pairs of those elements, zipWith allows us to provide a function that tells us how to combine the elements from the input lists. So let’s do zipWith using the max function, which will allow us to combine input elements of integer lists.
318.3
And in the output list, we’ll have the maximum of the corresponding two entries in the input lists.
326.2
So you see the two integer lists there, 1, 2, 3 and 0, 2, 4. Well, the max element in each of the list locations will be in the output list when zipWith is evaluated here. And indeed, we see the first element 1 is the greater of the two corresponding elements in the input list. And for the last element, 4 is the greater of the two elements in the corresponding input lists.
356.6
Let’s try another zipWith. Recall that if we specify an infix operator in parentheses, it becomes a prefix function. So we’re going to combine elements from 1, 2, 3 and 0, 2, 4 by adding them together, which should give us the list of the sums. 1 and 0 makes 1. 2 add 2 makes 4. 3 add 4 makes 7.
386.4
Now, let’s replicate the behavior of the zip function we saw earlier using the more general zipWith function we know about now.
401.1
Remember, zip took an element from the first input list and then an element from the second input list and then produced in the corresponding element in the output list the pair of those two elements from the input list. What I’ve written is a lambda expression to perform this operation. Take one element, another element, then produce a pair of elements.
437.5
We’ll look at lambda expressions in more detail later in the course. But for now, don’t worry about the details. So let’s try this zipWith function here with this lambda expression using a pair the lists we looked at earlier. And we see that the output is the same list of pairs.

The zip function is used to combine a pair of lists into a list of pairs. Since lists are such fundamental data structures in functional programming, it’s important to be able to manipulate them in flexible and powerful ways.

Does zip remind you of similar functions in other languages? Perhaps CONCAT in SQL? Please leave your suggestions in the comments.

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