1 Official Loop Notation

In order to provide convenient syntax for loops, two new keywords have been introduced in Mozart 1.1.0: for and do. Thus, a new statement is introduced in the Oz language and its syntax is:

for Declarations do ... end

where Declarations is a sequence of 0 or more iterator and feature declarations. An iterator has the form: Pat in Generator where Generator describes how to generate the successive values for pattern Pat whose variables are local to the loop. The generators are stepped in parallel and the loop terminates as soon as one of the generators runs out of values.

1.1 Iterators

These are the iterators officially supported starting in Mozart 1.1.1. For simplicity below, we write X rather than Pat.

in L

iterates over the elements of list L. At each iteration, X is bound to the next element in L. The generator runs out when all elements in L have been consumed.

in  E1;E2;E3
in (E1;E2;E3)

this iterator is intended to have a C-like flavor. E1 is the initial value, E2 is the boolean condition, and E3 is the next value. The iterator runs out when E2 evaluates to false.

in E1..E2;E3

iterate over the integers from E1 up to E2 inclusive, by increment of E3. E1, E2 and E3 are evaluated only once prior to starting the loop. Whether the loop is intended to count upward or downward is determined by the sign of E3.

in E1..E2

same as above, but with increment of 1.

in E1;E2

shorthand for in E1;true;E2

1.2 Features

Additional loop functionality is made available through the new notion of loop ``features''. For example, in C, it is possible to break or continue the current loop. We generalize this idea by making the ``break'' and ``continue'' capabilities first-class using nullary procedures: thus you can break or continue any loop from any arbitrary level of dynamic nesting.

break:B

binds B to a nullary procedure which, when invoked immediately breaks out of the corresponding loop. This is currently implemented using an exception, but may change in the future, which means that (1) if you indiscriminately catch all exceptions you may break this functionality, (2) if your code relies on catching ``break'' exceptions, it may not work in a future release.

continue:C

binds C to a nullary procedure which, when invoked immediately goes on to the next iteration of the loop. The same warning applies as above.


Denys Duchier
Version 1.4.0 (20080702)