The multiple-value feature was added in R5RS.
Function: call-with-values thunk receiver
Call its
thunkargument with a continuation that, when passed some values, calls thereceiverprocedure with those values as arguments.
Syntax: let-values ((formals expression) ...) body
Each
formalsshould be a formal arguments list as for alambda, cf section 4.1.4 of the R5RS.The
expressions are evaluated in the current environment, the variables of theformalsare bound to fresh locations, the return values of theexpressions are stored in the variables, thebodyis evaluated in the extended environment, and the values of the last expression ofbodyare returned. Thebodyis a "tail body", cf section 3.5 of the R5RS.The matching of each
formalsto values is as for the matching offormalsto arguments in alambdaexpression, and it is an error for anexpressionto return a number of values that does not match its correspondingformals.(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
formalsshould be a formal arguments list as for alambdaexpression, cf section 4.1.4 of the R5RS.
let*-valuesis similar tolet-values, but the bindings are performed sequentially from left to right, and the region of a binding indicated by (formalsexpression) is that part of thelet*-valuesexpression 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, andbodyare as described in R5RS. Specifically,formalscan have any of three forms:
- ‘
(’variable1...variablen)
The environment in which the receive-expression is evaluated is extended by binding
variable1, ...,variablento fresh locations. Theexpressionis evaluated, and its values are stored into those locations. (It is an error ifexpressiondoes not have exactlynvalues.)- ‘
’variable
The environment in which the receive-expression is evaluated is extended by binding
variableto a fresh location. Theexpressionis evaluated, its values are converted into a newly allocated list, and the list is stored in the location bound tovariable.- ‘
(’variable1...variablen.variablen+1)
The environment in which the receive-expression is evaluated is extended by binding
variable1, ...,variablen+1to fresh locations. Theexpressionis evaluated. Its firstnvalues are stored into the locations bound tovariable1...variablen. Any remaining values are converted into a newly allocated list, which is stored into the location bound tovariablen+1(It is an error ifexpressiondoes not have at leastnvalues.)In any case, the expressions in
bodyare evaluated sequentially in the extended environment. The results of the last expression in the body are the values of the receive-expression.