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

`do/2`

`(`

`+Iterator` do `+Body`)

`+Iterator`*iterator*, must be nonvar`:Body`*callable*, must be nonvar

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

, where`Tail`= []`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(`

. Iterate`MinExpr`)`Body`with`I`ranging over integers from`Min`.`I`is a local variable in`Body`. The termination condition is

, i.e.`I`=`Max``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(`

, let`MinExpr`)`Max`take the value`integer(`

, and let`MaxExpr`)`Past`take the value`max(`

. Iterate`Min`,`Max`+1)`Body`with`I`ranging over integers from`Min`to`max(`

inclusive.`Min`,`Max`)`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.

Call errors (see ref-sem-exc).

Send feedback on this subject.