Next: , Up: loops   [Index]


1.16.1 Introduction

An eager comprehension is a convenient notation for one or more nested or parallel loops generating a sequence of values, and accumulating this sequence into a result. In its most simple form, a comprehension looks like this:

(list-ec (: i 5)
  (* i i))
⇒ (0 1 4 9 16)

where i is a local variable that is sequentially bound to the values 0, 1, …, 4, and the squares of these numbers are collected in a list. The following example illustrates most conventions with respect to nesting and syntax:

(list-ec (: n 1 4)
         (: i n)
  (list n i))
⇒ ((1 0) (2 0) (2 1)
    (3 0) (3 1) (3 2))

the variable n is first bound to 1 then to 2 and finally to 3, and for each binding of n the variable i is bound to the values 0, 1, ..., n-1 in turn. The expression (list n i) constructs a two–elements list for each binding, and the comprehension list-ec collects all these results in a list.