10.2 Loops

The module Loop contains procedures that represent recursive versions of common iteration schemes with integers. However, for most common iteration patterns, the for loop offers a nicer alternative (see ``Loop Support'').

For

{Loop.'for' +I1 +I2 +I3 +P}

applies the unary procedure P to integers from I1 to I2 proceeding in steps of size I3. For example,

{For 1 11 3 Browse}

displays the numbers 1, 4, 7, and 10 in the browser window, whereas

{For 11 1 ~3 Browse}

displays the numbers 11, 8, 5, and 2.

ForThread

{Loop.forThread +I1 +I2 +I3 +P X ?Y}

applies the ternary procedure P to integers from I1 to I2 proceeding in steps of size I3 while threading an additional accumulator argument through the iteration. The procedure P takes the accumulator argument (initially set to X) and the loop index and returns an updated accumulator.

For example,

{ForThread 1 5 1 fun {$ Is I} I*I|Is end nil}

yields the list [25 16 9 4 1] as output, whereas

{ForThread 5 1 ~fun {$ Is I} I*I|Is end nil}

yields [1 4 9 16 25] as output.

Note that ForThread is similar to FoldL (see Section 6.3).

multiFor

{Loop.multiFor +Xs +P}

generalizes For (see above) to the case of multiple nested loops.

Xs is a list containing tuples of the form I1#I2#I3 specifying a loop by its start value I1, upper limit I2 and step size I3.

For example,

{Loop.multiFor [1#5#1 10#20#2] Browse}

displays the lists [1 10], [1 12], ..., [5 20] in the browser.

multiForThread

{Loop.multiForThread +Xs +P X ?Y}

generalizes ForThread (see above) to the case of multiple nested loops.

Xs is a list containing tuples of the form I1#I2#I3 specifying a loop by its start value I1, upper limit I2 and step size I3.

For example,

{Loop.multiForThread [1#2#1 5#4#~1]
 fun {$ Is [I J]}
    I#J|Is
 end nil}

yields the list [2#4 2#5 1#4 1#5] as output.


Denys Duchier, Leif Kornstaedt and Christian Schulte
Version 1.4.0 (20080702)