Next: , Previous: , Up: mpg-bpr   [Contents][Index]


11.3.67 do/2

Synopsis

(+Iterator do +Body)

Arguments

+Iterator

iterator, must be nonvar

:Body

callable, must be nonvar

Description

This control structure reduces the need to write auxiliary predicates performing simple iterations. It iterates Body until Iterator’s termination condition is true.

A iterator is a term of one of the following forms:

fromto(First,In,Out,Last)

In the first iteration, In=First. In the n:th iteration, In is the value that Out had at the end of the (n-1):th iteration. In and Out are local variables in Body. The termination condition is Out=Last.

foreach(X,List)

Iterate Body with X ranging over all elements of List. X is a local variable in Body. Can also be used for constructing a list. The termination condition is Tail = [], where Tail is the suffix of List that follows the elements that have been iterated over.

foreacharg(X,Struct)
foreacharg(X,Struct,I)

Iterate Body with X ranging over all arguments of Struct and I ranging over the argument number, 1-based. X and I are local variables in Body. Cannot be used for constructing a term. So the termination condition is true iff all arguments have been iterated over.

count(I,MinExpr,Max)

This is normally used for counting the number of iterations. Let Min take the value integer(MinExpr). Iterate Body with I ranging over integers from Min. I is a local variable in Body. The termination condition is I = Max, i.e. Max can be and typically is a variable.

for(I,MinExpr,MaxExpr)

This is used when the number of iterations is known. Let Min take the value integer(MinExpr), let Max take the value integer(MaxExpr), and let Past take the value max(Min,Max+1). Iterate Body with I ranging over integers from Min to max(Min,Max) inclusive. I is a local variable in Body. The termination condition is I = Past.

param(X)

For declaring variables in Body global, i.e. shared with the context. X can be a single variable, or a list of them. The termination condition is true. Please note: By default, variables in Body have local scope.

iterator,iterator

The iterators are iterated synchronously; that is, they all take their first value for the first execution of Body, their second value for the second execution of Body, etc. The order in which they are written does not matter, and the set of local variables in Body is the union of those of the iterators. The termination condition is the conjunction of those of the iterators.

Exceptions

Call errors (see ref-sem-exc).

See Also

ref-sem-ctr-dol.



Send feedback on this subject.