We start with a simple example:
> [X || X <- [1,2,a,3,4,b,5,6], X > 3].
[a,4,b,5,6]
This should be read as follows:
The list of X such that X is taken from the list
[1,2,a,...]
and X is greater than 3.
The notation X
<- [1,2,a,...]
is
a generator and the expression X
> 3
is
a filter.
An additional filter can be added in order to restrict the result to integers:
> [X || X <- [1,2,a,3,4,b,5,6], integer(X), X > 3].
[4,5,6]
Generators can be combined. For example, the Cartesian product of two lists can be written as follows:
> [{X, Y} || X <- [1,2,3], Y <- [a,b]].
[{1,a},{1,b},{2,a},{2,b},{3,a},{3,b}]