Multiple Return Values in Scheme
(define (vals)
(values 3 7))
(define (main)
; Here we use the 2 different return values from the
; call with multiple value binding
(let-values (((a b) (vals)))
(display a)
(newline)
(display b)
(newline))
; If you only want a subset of the returned values,
; use underscore as a placeholder
(let-values (((_ c) (vals)))
(display c)
(newline)))
(main)
Scheme has built-in support for multiple return values. This feature is used often in idiomatic Scheme, for example to return both result and error values from a function.
The (values 3 7)
in the vals
function shows that the function returns 2 values.
In the main
function, we use the 2 different return values from the call with multiple value binding using let-values
.
If you only want a subset of the returned values, use underscore _
as a placeholder.
To run the program, you would typically save it to a file (e.g., multiple-return-values.scm
) and run it with your Scheme interpreter. For example, if you’re using Guile:
$ guile multiple-return-values.scm
3
7
7
Returning multiple values is a powerful feature in Scheme that allows for more expressive and efficient code in many situations.