### 3.7 List comprehension expressions

Lists of ground terms can also be denoted by a list compehension expression. Examples are:

[X**2::X in L & not X in [0,1]]
% Squares of numbers other than 0,1 in nums list L
[C :: exists A child_of(C,peter) & age_of(C,A) & A<18]
% Non-adult children of peter in order found

The general form of a list comprehension is:

[Expression :: exists VarSequence SimpleConjunction]

where the `exists VarSequence`

is optional.

There are constraints on the variables that can be used in such a comprehension. Each variable in `Expression`

must either appear in `SimpleConjunction`

and be such that it will be given a ground value by some call in the conjunction, or it must appear before the comprehension expression in a query or rule and will have been given a ground value. Every variable in `SimpleConjunction`

must either be underscore, appear in `Expression`

or in `VarSequence`

, or must appear before the comprehension expression in a query or rule and will have been given a ground value. This ensures that the value of a list comprehension is always a list of ground terms.

`VarSequence`

is a single variable or a comma separated sequence of variables such as `X,Y,Z`

The syntax for `SimpleConjunction`

is given below.