5.4 Loops

A loop is an expression that contains another expression, loop called the loop body, which is to be evaluated zero or more loop:body times. All loops contain the repeat keyword and return the unique value of Void. Loops can contain inner loops to any depth.

The most basic loop is of the form

repeat loopBody

Unless loopBody contains a break or return expression, the loop

repeats forever. The value returned by the loop is the unique value of Void.

5.4.1 Compiling vs. Interpreting Loops

FriCAS tries to determine completely the type of every object in a loop and then to translate the loop body to LISP or even to machine code. This translation is called compilation.

If FriCAS decides that it cannot compile the loop, it issues a loop:compilation message stating the problem and then the following message:

We will attempt to step through and interpret the code.

It is still possible that FriCAS can evaluate the loop but in interpret-code mode. See section ugUserCompInt where this is discussed in terms panic:avoiding of compiling versus interpreting functions.

5.4.2 return in Loops

A return expression is used to exit a function with loop:leaving via return a particular value. In particular, if a return is in a loop within the return function, the loop is terminated whenever the return is evaluated.

Suppose we start with this.

f() ==
  i := 1
  repeat
    if factorial(i) > 1000 then return i
    i := i + 1

Type: Void

When factorial(i) is big enough, control passes from inside the loop all the way outside the function, returning the value of i (or so we think).

f()

Type: Void

What went wrong? Isn’t it obvious that this function should return an integer? Well, FriCAS makes no attempt to analyze the structure of a loop to determine if it always returns a value because, in general, this is impossible. So FriCAS has this simple rule: the type of the function is determined by the type of its body, in this case a block. The normal value of a block is the value of its last expression, in this case, a loop. And the value of every loop is the unique value of Void.! So the return type of f is Void.

There are two ways to fix this. The best way is for you to tell FriCAS what the return type of f is. You do this by giving f a declaration f:()->Integer prior to calling for its value. This tells FriCAS: trust me—an integer is returned. We’ll explain more about this in the next chapter. Another clumsy way is to add a dummy expression as follows.

Since we want an integer, let’s stick in a dummy final expression that is an integer and will never be evaluated.

f() ==
  i := 1
  repeat
    if factorial(i) > 1000 then return i
    i := i + 1
  0

Type: Void

When we try f again we get what we wanted. See ugUserBlocks for more information.

f()
Compiling function f with type () -> NonNegativeInteger
\[\]
7

Type: PositiveInteger

5.4.3 break in Loops

The break keyword is often more useful break in terminating loop:leaving via break a loop. A break causes control to transfer to the expression immediately following the loop. As loops always return the unique value of Void., you cannot return a value with break. That is, break takes no argument.

This example is a modification of the last example in the previous section ugLangLoopsReturn . Instead of using return, we’ll use break.

f() ==
  i := 1
  repeat
    if factorial(i) > 1000 then break
    i := i + 1
  i
Compiled code for f has been cleared.
1 old definition(s) deleted for function or rule f

Type: Void

The loop terminates when factorial(i) gets big enough, the last line of the function evaluates to the corresponding good value of i, and the function terminates, returning that value.

f()
   Compiling function f with type () -> PositiveInteger
+++ |*0;f;1;G82322| redefined
\[\]
7

Type: PositiveInteger

You can only use break to terminate the evaluation of one loop. Let’s consider a loop within a loop, that is, a loop with a nested loop. First, we initialize two counter variables.

(i,j) := (1, 1)
\[\]
1

Type: PositiveInteger

Nested loops must have multiple break loop:nested expressions at the appropriate nesting level. How would you rewrite this so (i + j) > 10 is only evaluated once?

repeat
  repeat
    if (i + j) > 10 then break
    j := j + 1
  if (i + j) > 10 then break
  i := i + 1

Type: Void

5.4.4 break vs. => in Loop Bodies

Compare the following two loops:

i := 1                            i := 1
repeat                            repeat
  i := i + 1                        i := i + 1
  i > 3 => i                        if i > 3 then break
  output(i)                         output(i)

In the example on the left, the values 2 and 3 for i are displayed but then the => does not allow control to reach the call to outputoutputOutputForm again. The loop will not terminate until you run out of space or interrupt the execution. The variable i will continue to be incremented because the => only means to leave the block, not the loop.

In the example on the right, upon reaching 4, the break will be executed, and both the block and the loop will terminate. This is one of the reasons why both => and break are provided. Using a while clause (see below) with the => while lets you simulate the action of break.

5.4.5 More Examples of break

Here we give four examples of repeat loops that terminate when a value exceeds a given bound.

First, initialize i as the loop counter.

i := 0
\[\]
0

Type: NonNegativeInteger

Here is the first loop. When the square of i exceeds 100, the loop terminates.

repeat
  i := i + 1
  if i^2 > 100 then break

Type: Void

Upon completion, i should have the value 11.

i
\[\]
11

Type: NonNegativeInteger

Do the same thing except use => instead an if-then expression.

i := 0
\[\]
0

Type: NonNegativeInteger

repeat
  i := i + 1
  i^2 > 100 => break

Type: Void

i
\[\]
11

Type: NonNegativeInteger

As a third example, we use a simple loop to compute n!.

(n, i, f) := (100, 1, 1)
\[\]
1

Type: PositiveInteger

Use i as the iteration variable and f to compute the factorial.

repeat
  if i > n then break
  f := f * i
  i := i + 1

Type: Void

Look at the value of f.

f
93326215443944152681699238856266700490715968264381621468_
59296389521759999322991560894146397615651828625369792082_
7223758251185210916864000000000000000000000000

Type: PositiveInteger

Finally, we show an example of nested loops. First define a four by four matrix.

m := matrix [ [21,37,53,14], [8,-24,22,-16], [2,10,15,14],

[26,33,55,-13] ]

\[\]
[213753148-2422-162101514263355-13]

Type: Matrix Integer

Next, set row counter r and column counter c to 1. Note: if we were writing a function, these would all be local variables rather than global workspace variables.

(r, c) := (1, 1)
\[\]
1

Type: PositiveInteger

Also, let lastrow and lastcol be the final row and column index.

(lastrow, lastcol) := (nrows(m), ncols(m))
\[\]
4

Type: PositiveInteger

Scan the rows looking for the first negative element. We remark that you can reformulate this example in a better, more concise form by using a for clause with repeat. See ugLangLoopsForIn for more information.

repeat
  if r > lastrow then break
  c := 1
  repeat
    if c > lastcol then break
    if elt(m,r,c) < 0 then
      output [r, c, elt(m,r,c)]
      r := lastrow
      break     -- don't look any further
    c := c + 1
  r := r + 1

   [2,2,- 24]

Type: Void

5.4.6 iterate in Loops

FriCAS provides an iterate expression that iterate skips over the remainder of a loop body and starts the next loop iteration.

We first initialize a counter.

i := 0
\[\]
0

Type: NonNegativeInteger

Display the even integers from 2 to 5.

repeat
  i := i + 1
  if i > 5 then break
  if odd?(i) then iterate
  output(i)

   2
   4

Type: Void

5.4.7 while Loops

The repeat in a loop can be modified by adding one or more while clauses. while Each clause contains a predicate immediately following the while keyword. The predicate is tested before the evaluation of the body of the loop. The loop body is evaluated whenever the predicates in a while clause are all true.

The syntax for a simple loop using while is

while predicate repeat loopBody

The predicate is evaluated before loopBody is evaluated. A while loop

terminates immediately when predicate evaluates to false or when a break or return expression is evaluated in loopBody. The value returned by the loop is the unique value of Void.

Here is a simple example of using while in a loop. We first initialize the counter.

i := 1
\[\]
1

Type: PositiveInteger

The steps involved in computing this example are
(1) set i to 1,
(2) test the condition i<1 and determine that it is not true, and
(3) do not evaluate the loop body and therefore do not display

“hello”.

while i < 1 repeat
  output "hello"
  i := i + 1

Type: Void

If you have multiple predicates to be tested use the logical and operation to separate them. FriCAS evaluates these predicates from left to right.

(x, y) := (1, 1)
\[\]
1

Type: PositiveInteger

while x < 4 and y < 10 repeat
  output [x,y]
  x := x + 1
  y := y + 2

   [1,1]
   [2,3]
   [3,5]

Type: Void

A break expression can be included in a loop body to terminate a loop even if the predicate in any while clauses are not false.

(x, y) := (1, 1)
\[\]
1

Type: PositiveInteger

This loop has multiple while clauses and the loop terminates before any one of their conditions evaluates to false.

while x < 4 while y < 10 repeat
  if x + y > 7 then break
  output [x,y]
  x := x + 1
  y := y + 2

   [1,1]
   [2,3]

Type: Void

Here’s a different version of the nested loops that looked for the first negative element in a matrix.

m := matrix [ [21,37,53,14], [8,-24,22,-16], [2,10,15,14],

[26,33,55,-13] ]

\[\]
[213753148-2422-162101514263355-13]

Type: Matrix Integer

Initialized the row index to 1 and get the number of rows and columns. If we were writing a function, these would all be local variables.

r := 1
\[\]
1

Type: PositiveInteger

(lastrow, lastcol) := (nrows(m), ncols(m))
\[\]
4

Type: PositiveInteger

Scan the rows looking for the first negative element.

while r <= lastrow repeat
  c := 1  -- index of first column
  while c <= lastcol repeat
    if elt(m,r,c) < 0 then
      output [r, c, elt(m,r,c)]
      r := lastrow
      break     -- don't look any further
    c := c + 1
  r := r + 1

   [2,2,- 24]

Type: Void

5.4.8 for Loops

FriCAS provides the for for and in  in keywords in repeat loops, allowing you to iterate across all iteration elements of a list, or to have a variable take on integral values from a lower bound to an upper bound. We shall refer to these modifying clauses of repeat loops as for clauses. These clauses can be present in addition to while clauses. As with all other types of repeat loops, break can break be used to prematurely terminate the evaluation of the loop.

The syntax for a simple loop using for is

for iterator repeat loopBody

The iterator has several forms. Each form has an end test which is evaluated before loopBody is evaluated. A for loop terminates immediately when the end test succeeds (evaluates to true) or when a break or return expression is evaluated in loopBody. The value returned by the loop is the unique value of Void.\

5.4.9 for i in n..m repeat

If for for is followed by a variable name, the in  in keyword and then an integer segment of the form n..m, segment the end test for this loop is the predicate i>m. The body of the loop is evaluated m-n+1 times if this number is greater than 0. If this number is less than or equal to 0, the loop body is not evaluated at all.

The variable i has the value n,n+1,...,m for successive iterations of the loop body.The loop variable is a local variable within the loop body: its value is not available outside the loop body and its value and type within the loop body completely mask any outer definition of a variable with the same name.

This loop prints the values of 103, 113, and 123:

for i in 10..12 repeat output(i^3)
1000
1331
1728

Type: Void

Here is a sample list.

a := [1,2,3]
\[\]
[1,2,3]

Type: List PositiveInteger

Iterate across this list, using . to access the elements of a list and the # operation to count its elements.

for i in 1.. #a repeat output(a.i)
1
2
3

Type: Void

This type of iteration is applicable to anything that uses .. You can also use it with functions that use indices to extract elements.

Define m to be a matrix.

m := matrix [ [1,2],[4,3],[9,0] ]
\[\]
[124390]

Type: Matrix Integer

Display the rows of m.

for i in 1..nrows(m) repeat output row(m,i)
[1,2]
[4,3]
[9,0]

Type: Void

You can use iterate with for-loops.iterate

Display the even integers in a segment.

for i in 1..5 repeat
  if odd?(i) then iterate
  output(i)

   2
   4

Type: Void

See section SegmentXmpPage for more information about segments.

5.4.10 for i in n..m by s repeat

By default, the difference between values taken on by a variable in loops such as for i in n..m repeat ... is 1. It is possible to supply another, possibly negative, step value by using the by by keyword along with for and in . Like the upper and lower bounds, the step value following the by keyword must be an integer. Note that the loop for i in 1..2 by 0 repeat output(i) will not terminate by itself, as the step value does not change the index from its initial value of 1.

This expression displays the odd integers between two bounds.

for i in 1..5 by 2 repeat output(i)
1
3
5

Type: Void

Use this to display the numbers in reverse order.

for i in 5..1 by -2 repeat output(i)
5
3
1

Type: Void

5.4.11 for i in n.. repeat

If the value after the .. is omitted, the loop has no end test. A potentially infinite loop is thus created. The variable is given the successive values n,n+1,n+2,... and the loop is terminated only if a break or return expression is evaluated in the loop body. However you may also add some other modifying clause on the repeat (for example, a while clause) to stop the loop.

This loop displays the integers greater than or equal to 15 and less than the first prime greater than 15.

for i in 15.. while not prime?(i) repeat output(i)
15
16

Type: Void

5.4.12 for x in l repeat

Another variant of the for loop has the form:

for x in  list repeat loopBody

This form is used when you want to iterate directly over the elements of a list. In this form of the for loop, the variable x takes on the value of each successive element in l. The end test is most simply stated in English: are there no more x in l?

If l is this list,

l := [0,-5,3]
\[\]
[0,-5,3]

Type: List Integer

display all elements of l, one per line.

for x in l repeat output(x)
0
- 5
3

Type: Void

Since the list constructing expression expand[n..m] creates the list [n,n+1,...,m]. Note that this list is empty if n>m. You might be tempted to think that the loops

for i in n..m repeat output(i)

and

for x in expand [n..m] repeat output(x)

are equivalent. The second form first creates the list expand[n..m] (no matter how large it might be) and then does the iteration. The first form potentially runs in much less space, as the index variable i is simply incremented once per loop and the list is not actually created. Using the first form is much more efficient.

Of course, sometimes you really want to iterate across a specific list. This displays each of the factors of 2400000.

for f in factors(factor(2400000)) repeat output(f)
[factor= 2,exponent= 8]
[factor= 3,exponent= 1]
[factor= 5,exponent= 5]

Type: Void

5.4.13 Such that Predicates

A for loop can be followed by a | and then a predicate. The predicate qualifies the use of the values from the iterator following the for. Think of the vertical bar | as the phrase such that.

This loop expression prints out the integers n in the given segment such that n is odd.

for n in 0..4 | odd? n repeat output n
1
3

Type: Void

A for loop can also be written foriterator|predicaterepeatloopBody

which is equivalent to: for iterator repeat if predicate then loopBody else iterate

The predicate need not refer only to the variable in the for clause: any variable in an outer scope can be part of the predicate.

In this example, the predicate on the inner for loop uses i from the outer loop and the j from the for iteration:nested clause that it directly modifies.

for i in 1..50 repeat
  for j in 1..50 | factorial(i+j) < 25 repeat
    output [i,j]

   [1,1]
   [1,2]
   [1,3]
   [2,1]
   [2,2]
   [3,1]

Type: Void

5.4.14 Parallel Iteration

The last example of the previous section ugLangLoopsForInPred gives an example of nested iteration: a loop is contained iteration:nested in another loop. iteration:parallel Sometimes you want to iterate across two lists in parallel, or perhaps you want to traverse a list while incrementing a variable.

The general syntax of a repeat loop is

iterator1iterator2…iteratorN repeat loopBody where each iterator is either a for or a while clause. The loop terminates immediately when the end test of any iterator succeeds or when a break or return expression is evaluated in loopBody. The value returned by the loop is the unique value of Void.

Here we write a loop to iterate across two lists, computing the sum of the pairwise product of elements. Here is the first list.

l := [1,3,5,7]
\[\]
[1,3,5,7]

Type: List PositiveInteger

And the second.

m := [100,200]
\[\]
[100,200]

Type: List PositiveInteger

The initial value of the sum counter.

sum := 0
\[\]
0

Type: NonNegativeInteger

The last two elements of l are not used in the calculation because m has two fewer elements than l.

for x in l for y in m repeat
    sum := sum + x*y

Type: Void

Display the dot product.

sum
\[\]
700

Type: NonNegativeInteger

Next, we write a loop to compute the sum of the products of the loop elements with their positions in the loop.

l := [2,3,5,7,11,13,17,19,23,29,31,37]
\[\]
[2,3,5,7,11,13,17,19,23,29,31,37]

Type: List PositiveInteger

The initial sum.

sum := 0
\[\]
0

Type: NonNegativeInteger

Here looping stops when the list l is exhausted, even though the foriin0.. specifies no terminating condition.

for i in 0.. for x in l repeat sum := i * x

Type: Void

Display this weighted sum.

sum
\[\]
407

Type: NonNegativeInteger

When | is used to qualify any of the for clauses in a parallel iteration, the variables in the predicates can be from an outer scope or from a for clause in or to the left of a modified clause.

This is correct:

for i in 1..10 repeat
  for j in 200..300 | odd? (i+j) repeat
    output [i,j]

This is not correct since the variable j has not been defined outside the inner loop.

for i in 1..10 | odd? (i+j) repeat  -- wrong, j not defined
  for j in 200..300 repeat
    output [i,j]

5.4.15 Mixing Loop Modifiers

This example shows that it is possible to mix several of the loop:mixing modifiers forms of repeat modifying clauses on a loop.

for i in 1..10
    for j in 151..160 | odd? j
      while i + j < 160 repeat
        output [i,j]

   [1,151]
   [3,153]

Type: Void

Here are useful rules for composing loop expressions:

  1. while predicates can only refer to variables that are global (or in an outer scope) or that are defined in for clauses to the left of the predicate.
  2. A such that predicate (something following |) must directly follow a for clause and can only refer to variables that are global (or in an outer scope) or defined in the modified for clause or any for clause to the left.