[next][up][top][index]
search for:

loops -- evaluate code repeatedly

One good way to perform an operation several times is with the keyword while. An expression of the form while X do Y operates by evaluating X repeatedly. Each time the value of X is true, Y is evaluated and its value is discarded. When finally the value of X is false the special value null is returned as the value of the while expression.

i1 : i = 0;
i2 : while i < 20 do (<< " " << i; i = i + 1)
 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

In the example above, X is the predicate i < 20 and Y is the code (<< " " << i; i = i + 1). Notice the use of the semicolon within Y to separate two expressions.

The semicolon can also be used within the predicate X to do other things before the test is performed. This works because the value of an expression of the form (A;B;C;D;E) is obtained by evaluating each of the parts, and providing the value of the last part (in this case, E), as the value of the whole expression. Thus, if the value of E is always true or false, the expression (A;B;C;D;E) can be used as the predicate X. We illustrate this in the following example.

i3 : i = 0;
i4 : while (<< " " << i; i < 20) do i = i+1
 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

If we use the form while X list Y then the final value of the expression is a list of all the values of Y encountered.

i5 : i = 1; while (i = 2*i; i < 100) list i

o6 = {2, 4, 8, 16, 32, 64}

o6 : List

The two keywords can be combined in an expression of the form while X list Y do Z, in which case Y and Z are both evaluated each time, and the final value is a list of all the values of Y encountered.

i7 : i = 1; while i < 100 list i do i = 2*i

o8 = {1, 2, 4, 8, 16, 32, 64}

o8 : List
i9 : i = List; while i =!= Thing list i do i = parent i

o10 = {List, VisibleList, BasicList}

o10 : List

The keyword break can be used to terminate a loop early, and optionally to specify a return value for the while-expression.

i11 : i = 0; while true do (j = i!; if j > 1000000 then break j else i = i+1)

o12 = 3628800

Another good way to perform an operation several times is with the keyword for, especially when we are looping over consecutive integers, as with the variable i in the previous example. Here is the same computation, implemented with for.

i13 : for i do (k := i!; if k > 1000000 then break k)

o13 = 3628800

Note: a for-loop starts a new lexical scope for local variables, and hence the value of k is not known outside the loop; see :=.

The keyword when can be used with for to specify a predicate which must remain true for execution to continue, and the keyword list can be used to specify values which should be accumulated into a list and return as the value of the for-loop. The keywords from and to can be used to specify numerical limits for the loop variable. Here is an example that illustrate all of these keywords at once.

i14 : for i from 10 to 30 when i<15 list 100*i do print i
10
11
12
13
14

o14 = {1000, 1100, 1200, 1300, 1400}

o14 : List


[next][up][top][index]
search for: