Chapter 4

Program Control

Iteration

Iteration is supported through a number of statements, as well as through recursive functions.

Iteration Statements

The statements supporting iteration are described in detail in Chapter 14, The Built-In Macros and Special Definitions.

Table 4-3 Iteration Statements

Macro

Description

Page

while

Repeatedly executes a body until a test expression is false.

399

until

Repeatedly executes a body until a test expression is true.

400

for

Performs general iteration over a body, updating bindings and performing end tests on each iteration.

400

Tail Recursion

Implementations are encouraged to optimize tail recursive function calls whenever possible. Tail recursion occurs when a function F1 returns the values of a call to another function F2. In many cases, this can be used to create loops using self-recursive or mutually recursive functions. (Among the cases that cannot be optimized are those in which the return value types of F1 and F2 differ, requiring the F1 to check the types of the values before returning them.)

The following example uses tail recursion to compute the name of the root volume on which a given file system object is stored.

define method root-volume-name (f :: <file-or-directory>)
  if ( root-volume?(f) ) 
    f.name
  else 
    root-volume-name(f.container)
  end if;
end method;

The example above can execute with constant stack size, regardless of how deeply nested the file system hierarchy may be.