Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Typical of operations on lists are mapping—applying a function to every element of a list—and filtering—selecting those elements of a list that have a particular property. Often these are used together, and the list comprehension notation gives you a powerful and compact way to write down lists constructed in this way. We’ll begin with some simple examples, and then look in more detail at a simple database case study.
The list comprehension in the following code has the generator X <- [1,2,3], which means X runs through the values 1, 2, and 3 in turn. Going into the output for each of these is X+1, the expression before the symbol ||, 1 leading to 2, and so on. This gives the result [2,3,4].