Next: srfi eager-comp rationale, Previous: srfi eager-comp license, Up: srfi eager-comp [Index]
This SRFI defines a modular and portable mechanism for eager comprehensions extending the algorithmic language Scheme. 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 according to this SRFI looks like this:
(list-ec (: i 5) (* i i)) => (0 1 4 9 16)
Here, 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 of this SRFI 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))
In the example, 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–element list for each bindings, and
the comprehension list-ec collects all these results in a list.
The mechanism defined in this SRFI has the following properties:
list-ec, append-ec, sum-ec, min-ec,
every?-ec, do-ec, and others. Some other natural
comprehensions (e.g. gcd-ec) have not been included into this
SRFI due to their low significance for most applications. On the
other hand, a few comprehensions (fold-ec, fold3-ec) not
inspired by R5RS have been included due to their broad
applicability.
:list, :string, …)
expecting certain types of objects for their arguments. These
generators usually produce code as efficient as hand coded
do–loops.
: (read “run through”)
dispatching on the value of its argument list at runtime. In the
examples above, one or two integers were used to define a range. The
convenience of omitting the type comes at a certain performance penalty,
both per iteration and during startup of the loop.
:parallel), and can be stopped early before (:while) or
after (:until) producing the current value.
if, not,
and, or), intermediate commands can be evaluated between
generators (begin), and intermediate variables can be introduced
(:let).
[outer .. inner | expr]
convention, meaning that the most right generator (inner) spins
fastest and is followed by the result expression over which the
comprehension ranges (expr). srfi eager-comp design for
details. Moreover, the syntax is strictly prefix and the naming
convention my-comprehension-ec, :my-generator is used
systematically.
Next: srfi eager-comp rationale, Previous: srfi eager-comp license, Up: srfi eager-comp [Index]