Skip to 0 minutes and 5 secondsJEREMY: Hello everyone. Time for some more functional programming. Let's go.

Skip to 0 minutes and 13 secondsYou 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.

Skip to 0 minutes and 41 secondsHere, 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.

Skip to 1 minute and 1 secondIt's the same with the second two elements from the lists. Bread and butter belong together as a pair.

Skip to 1 minute and 10 secondsAnd 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.

Skip to 1 minute and 28 secondsHi. Now, we're going to evaluate some zip function calls inside the Haskell interpreter, GHCi.

Skip to 1 minute and 40 secondsIf 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.

Skip to 2 minutes and 24 secondsI 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.

Skip to 2 minutes and 53 secondsYou 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.

Skip to 3 minutes and 34 secondsConveniently, 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.

Skip to 4 minutes and 23 secondsSo 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.

Skip to 4 minutes and 46 secondsNow, 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.

Skip to 5 minutes and 18 secondsAnd in the output list, we'll have the maximum of the corresponding two entries in the input lists.

Skip to 5 minutes and 26 secondsSo 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.

Skip to 5 minutes and 57 secondsLet'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.

Skip to 6 minutes and 26 secondsNow, let's replicate the behavior of the zip function we saw earlier using the more general zipWith function we know about now.

Skip to 6 minutes and 41 secondsRemember, 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.

Skip to 7 minutes and 18 secondsWe'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.

Zip that List

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.

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: