List Comprehension
Learn how to build lists using list comprehensions.
We'll cover the following
In the final lesson on lists, we take a look at another powerful syntactic element to build complex lists: the list comprehension syntax.
List comprehensions by example
List comprehensions are a concept inspired by set definitions in mathematical notation. In mathematics, the definition
defines the set of even squares. It is advised to start reading such a set definition from the right of the separating pipe symbol |
.
- The elements are drawn from the set of natural numbers .
- Only the even are kept.
- Finally (to the left of the pipe
|
), the set consists of the squares of these even .
Similarly, we can define a list of the even squares in Haskell by using the closely related list comprehension syntax.
Get hands-on with 1200+ tech skills courses.