Next: , Previous: , Up: loops   [Index]


1.16.3 Qualifiers

This section defines the syntax ?qualifier. The nesting of qualifiers is from left (outer) to right (inner). In other words, the rightmost generator “spins faster”. The nesting also defines the region of the variables introduced by the generators. This implies that inner generators may depend on the variables of outer generators. The sequence of enumeration of values is strictly depth first. These conventions are illustrated by the first example.

The syntax ?qualifier consists of the following alternatives.

Qualifier Syntax: ?generator

Enumerate a sequence of bindings of one or more variables. The region of the variables starts at the generator and extends over all subsequent qualifiers and expressions in the comprehension (see loops generators).

Qualifier Syntax: if test

Filter the sequence of bindings by testing if test evaluates to true. Only for those bindings for which this is the case, the subsequent qualifiers of the comprehension are evaluated.

(list-ec (:range i 10)
         (if (even? i))
  i)
⇒ (0 2 4 6 8)

(list-ec (:range i 5)
         (if (even? i))
         (:let j (+ 1 i))
  j)
⇒ (1 3 5)
Qualifier Syntax: not test
Qualifier Syntax: and test ...
Qualifier Syntax: or test ...

Abbreviated notations for filters of the form:

(if (not test))
(if (and test ...))
(if (or  test ...))
Qualifier Syntax: begin sequence

Evaluate sequence, consisting of ‘command ... expression’, once for each binding of the variables defined by the previous qualifiers in the comprehension. Using this qualifier, side effects can be inserted into the body of a comprehension.

(let* ((ans '())
       (ell (list-ec (:range i 5)
                     (begin
                       (set! ans (cons i ans)))
                     i)))
  (list ans ell))
⇒ ((4 3 2 1 0)
    (0 1 2 3 4))
Qualifier Syntax: nested ?qualifier ...

A syntactic construct to group qualifiers. The meaning of a qualifier according to the ‘nested’ syntax is the same as inserting the ?qualifier syntaxes into the enclosing comprehension.

This construct can be used to reduce comprehensions with several qualifiers into a form with exactly one qualifier.

(list-ec (nested (:range i 5)
                 (if (even? i))
                 (:let j (+ 1 i)))
  j)
⇒ (1 3 5)

(list-ec (:range i 5)
         (nested (if (even? i))
                 (:let j (+ 1 i)))
  j)
⇒ (1 3 5)

(list-ec (:range i 5)
         (if (even? i))
         (nested (:let j (+ 1 i)))
  j)
⇒ (1 3 5)

(list-ec (:range i 5)
         (if (even? i))
         (:let j (+ 1 i))
         (nested)
  j)
⇒ (1 3 5)

Next: , Previous: , Up: loops   [Index]