Multiple values

The multiple-value feature was added in R5RS.

Function: values object ...

Delivers all of its arguments to its continuation.

Function: call-with-values thunk receiver

Call its thunk argument with a continuation that, when passed some values, calls the receiver procedure with those values as arguments.

Syntax: let-values ((formals expression) ...) body

Each formals should be a formal arguments list as for a lambda, cf section 4.1.4 of the R5RS.

The expressions are evaluated in the current environment, the variables of the formals are bound to fresh locations, the return values of the expressions are stored in the variables, the body is evaluated in the extended environment, and the values of the last expression of body are returned. The body is a "tail body", cf section 3.5 of the R5RS.

The matching of each formals to values is as for the matching of formals to arguments in a lambda expression, and it is an error for an expression to return a number of values that does not match its corresponding formals.

(let-values (((a b . c) (values 1 2 3 4)))
  (list a b c))            --> (1 2 (3 4))

(let ((a 'a) (b 'b) (x 'x) (y 'y))
  (let-values (((a b) (values x y))
               ((x y) (values a b)))
    (list a b x y)))       --> (x y a b)

Syntax: let*-values ((formals expression) ...) body

Each formals should be a formal arguments list as for a lambda expression, cf section 4.1.4 of the R5RS.

let*-values is similar to let-values, but the bindings are performed sequentially from left to right, and the region of a binding indicated by (formals expression) is that part of the let*-values expression to the right of the binding. Thus the second binding is done in an environment in which the first binding is visible, and so on.

(let ((a 'a) (b 'b) (x 'x) (y 'y))
  (let*-values (((a b) (values x y))
                ((x y) (values a b)))
    (list a b x y)))       --> (x y x y)

Syntax: receive formals expression body

The formals, expression, and body are as described in R5RS. Specifically, formals can have any of three forms:

(variable1 ... variablen)

The environment in which the receive-expression is evaluated is extended by binding variable1, ..., variablen to fresh locations. The expression is evaluated, and its values are stored into those locations. (It is an error if expression does not have exactly n values.)

variable

The environment in which the receive-expression is evaluated is extended by binding variable to a fresh location. The expression is evaluated, its values are converted into a newly allocated list, and the list is stored in the location bound to variable.

(variable1 ... variablen . variablen+1)

The environment in which the receive-expression is evaluated is extended by binding variable1, ..., variablen+1 to fresh locations. The expression is evaluated. Its first n values are stored into the locations bound to variable1 ... variablen. Any remaining values are converted into a newly allocated list, which is stored into the location bound to variablen+1 (It is an error if expression does not have at least n values.)

In any case, the expressions in body are evaluated sequentially in the extended environment. The results of the last expression in the body are the values of the receive-expression.

Function: values-append arg1 ...

The values resulting from evaluating each argument are appended together.