[Next] [Previous] [Up] [Top] [Contents] [Index]

4 Program Control

Iteration

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

Iteration Statements

The statemens supporting iteration are described in detail in Chapter 14, "The Built-In Macros and Special Operators."
Iteration Statements
Macro DescriptionPage
while Repeatedly executes a body until a test expression is false.386
until Repeatedly executes a body until a test expression is true.386
for Performs general iteration over a body, updating bindings and performing end tests on each iteration.387

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 which 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.


Dylan Reference Manual - 17 OCT 1995
[Next] [Previous] [Up] [Top] [Contents] [Index]

Generated with Harlequin WebMaker