Control structures

Library (rnrs control (6))

The (rnrs control (6))library, which provides useful control structures.

Syntax when test expression ...
Syntax unless test expression ...

[R6RS] Test must be an expression.

A when expression is evaluated by evaluating the test expression. If test evaluates to a true value, the remaining expressions are evaluated in order, and the results of the last expression are returned as the results of the entire when expression. Otherwise, the whenexpression returns unspecified values. An unless expression is evaluated by evaluating the test expression. If test evaluates to #f, the remaining expressions are evaluated in order, and the results of the last expression are returned as the results of the entire unlessexpression. Otherwise, the unless expression returns unspecified values.

Syntax do ((variable init step) ... ) (test expression ... ) commend

[R6RS] The inits, steps, tests, and commands must be expressions. The variables must be pairwise distinct variables.

The do expression is an iteration construct. It specifies a set of variables to be bound, how they are to be initialized at the start, and how they are to be updated on each iteration.

A do expression is evaluated as follows: The init expressions are evaluated (in some unspecified order), the variables are bound to fresh locations, the results of the init expressions are stored in the bindings of the variables, and then the iteration phase begins.

Each iteration begins by evaluating test if the result is #f, then the commands are evaluated in order for effect, the step expressions are evaluated in some unspecified order, the variables are bound to fresh locations holding the results, and the next iteration begins.

If test evaluates to a true value, the expressions are evaluated from left to right and the values of the last expression are returned. If no expressions are present, then the do expression returns unspecified values.

The region of the binding of a variable consists of the entire doexpression except for the inits.

A step may be omitted, in which case the effect is the same as if (variable init variable) had been written instead of (variable init).

(do ((vec (make-vector 5))
      (i 0 (+ i 1)))
     ((= i 5) vec)
  (vector-set! vec i i))
#(0 1 2 3 4)

(let ((x '(1 3 5 7 9)))
  (do ((x x (cdr x))
        (sum 0 (+ sum (car x))))
       ((null? x) sum)))
25

Syntax case-lambda case-lambda-clause ...

[R6RS] Each case-lambda-clause must be of the form

(_formals_ _body_)

Formals must be as in a lambda form.

A case-lambda expression evaluates to a procedure. This procedure, when applied, tries to match its arguments to the case-lambda-clauses in order. The arguments match a clause if one of the following conditions is fulfilled:

Formals has the form (_variable_ ...) and the number of arguments is the same as the number of formal parameters in formals.

Formals has the form

(_variable1_ ... _variablen_ . _variablen+1_)

and the number of arguments is at least n.

Formals has the form variable.

For the first clause matched by the arguments, the variables of the _formals_are bound to fresh locations containing the argument values in the same arrangement as with lambda.

The last expression of a body in a case-lambda expression is in tail context.

If the arguments match none of the clauses, an exception with condition type &assertion is raised.