[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Multiple values, version 1



This probably goes in section 6.9, "Control features".

(values obj1 ...)                                                    procedure

Returns its arguments as "multiple return values".  Most continuations ignore
all but the first return value, but the with-values procedure can create a
continuation that will use all the values being returned.  It is an error to
return zero values to an continuation that expects one.  See with-values.

  (let ((x (values 'a 'b 'c 'd))) x)                        ==>  a
  (let ((x (values))) x)                                    ==>  error


(with-values thunk proc)                                             procedure

Thunk must be a procedure of no arguments and proc must be a procedure.  The
thunk is called with no arguments, and all of its return values, not just the
first, are passed as arguments to proc.  See values.

  (with-values (lambda () (values 3 4 5)) +)                ==>  12
  (with-values (lambda () (values 'a 'b 'c)) list)          ==>  (a b c)
  (with-values (lambda () (values 'a)) list)                ==>  (a)
  (with-values (lambda () (values)) list)                   ==>  ()
  (with-values (lambda () (values))
               (lambda (x) (list x)))                       ==>  error
  (letrec ((stats (lambda (name)
                    (case name
                      ((henry)  (values 480 100 14 0 4))
                      ((george) (values 400 90 20 2 5))
                      (else     (values 0 0 0 0 0))))))
    (with-values (lambda () (stats 'henry))
                 (lambda (atbats singles doubles triples homeruns)
                   (/ (+ singles (* 2 doubles) (* 3 triples) (* 4 homeruns))
                      (max 1 atbats)))))
                                                            ==>  3/10