
Next: Additional Facilities
Up: Programming Constructs
Previous: Dynamic Wind
MzScheme supports mutliple return values through values,
call-with-values, and let-values.
Multiple values can also be returned by applying a continuation to
multiple arguments.
- (values arg
) returns the given args
as mulitple values. If args is not a single value, the result
of this expression must be used in a multiple-value context, but
(values v) is always equivalent to v.
- (call-with-values producer consumer) invokes
producer and passes the result to consumer. The
producer argument must be a procedure that takes no arguments and
returns one or more values. The consumer argument must be a
procedure that takes the same number of arguments as the number of
values returned by producer. The result of the
call-with-values expression is the result of applying
consumer.
- (let-values ([binding value]
) body)
is a binding form for multiple return values. Each binding
specifies identifiers to receieve the multiple return values of
value. If binding is an identifier, then the mulitply-returned
values are assigned to the variable in a list. If binding is a
list of identifiers, then each identifier receives one of the multiply
returned values; the number of identifiers in bindings must then
match the number of values returned by values. An ``improper'' list
can be used for binding (as in the formal arguments of a
lambda form) to collect extra return values into a list. If
mulitple binding-values pairs are given, the bindings from
one pair are visible in the next pair (similar to let*).
When multiple values are returned to a single value context, the
exn:application:arity exception is raised.
Examples:
(values 1) ; => 1
(values 1 2) ; => exn:application:arity - returned 2 values to single-value context
(values) ; => exn:application:arity - returned 0 values to single-value context
(call-with-values
(lambda () (values 1 2))
(lambda (x y) y)) ; => 2
(let-values ([(x y) (values 1 2)]) y) ; => 2
(let-values ([z (values 1 2)]) z) ; => (1 2)
(let-values ([z (values 1 2)]
[(x y) (apply values z)])
y) ; => 2
(let-values ([(x y) (let/cc k (k 3 4))]) y) ; => 4
(call-with-values
(lambda () (values 'hello 1 2 3 4))
(lambda (s . l)
(format " s = s" s l))) ; => "hello = (1 2 3 4)"

Next: Additional Facilities
Up: Programming Constructs
Previous: Dynamic Wind
PLT