Search code examples
lispracketmultiple-return-values

How can I bind Racket's multiple return values to global variable names using define?


Given a simple function that returns one value, I can use define to bind its return value to a name

(define (foo) 1)
(define bar (foo))

however this doesn't seem to be possible when the function returns multiple values using values.

(define (baz) (values 2 3))
(define quux (baz))
  ; define-values: result arity mismatch;
  ;  expected number of values not received
  ;   expected: 1
  ;   received: 2
  ;   from: 
  ;   in: definition of quux
  ;   values...:
  ;    2
  ;    3

I've looked through the docs, but the only thing related to values is let-values, which doesn't really apply here.


Solution

  • You want define-values, the definition equivalent to let-values.

    (define (baz) (values 2 3))
    (define-values (foo bar) (baz))