Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

9.3. List Comprehensions

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.

9.3.1. A First Example

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].


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial