fold-ec

[syntax] fold-ec x0 qualifier ... expression f2

[syntax] fold3-ec x0 qualifier ... expression f1 f2

SRFI-42: Reduces the sequence x[0], x[1], ..., x[n-1] of values obtained by evaluating expression once for each binding as specified by qualifier .. .. The arguments x0, f2, and f1, all syntactically equivalent to expression, specify the reduction process.

The reduction process for fold-ec is defined as follows. A reduction variable x is initialized to the value of x0, and for each k in {0, ..., n-1} the command (set! x (f2 x[k] x)) is evaluated. Finally, x is returned as the value of the comprehension.

The reduction process for fold3-ec is different. If and only if n = 0, i.e. the sequence is empty, then x0 is evaluated and returned as the value of the comprehension. Otherwise, a reduction variable x is initialized to the value of (f1 x[0]), and for each k in {1, ..., n-1} the command (set! x (f2 x[k] x)) is evaluated. Finally, x is returned as the value of the comprehension.

As the order of the arguments suggests, x0 is evaluated outside the scope of the qualifiers, whereas the reduction expressions involving f1 and f2 are inside the scope of the qualifiers (so they may depend on any variable introduced by the qualifiers). Note that f2 is evaluated repeatedly, with any side-effect or overhead this might have.