Scheme multiple values
last modified November 24, 2026 Guile 3.0
A procedure often computes more than one result: the quotient and the
remainder of a division, the smallest and the largest element of a list, a
parsed value and the input that is left over. It could return them in a list,
but then the caller has to take the list apart again. Scheme lets a procedure
return several values directly with values, and offers several
forms to receive them. The examples run with GNU Guile 3.0.7.
values and call-with-values
(values a b ...) returns all its arguments as separate values.
The basic way to accept them is call-with-values. It takes a
producer, a procedure without arguments that returns the values,
and a consumer, which is called with the values as its arguments.
(define (min-max numbers)
(values (apply min numbers) (apply max numbers)))
(call-with-values
(lambda () (min-max '(7 3 9 1 4)))
(lambda (low high)
(display "lowest: ")
(display low)
(display ", highest: ")
(display high)
(newline)))
(display (call-with-values (lambda () (min-max '(7 3 9 1 4))) -))
(newline)
(display (call-with-values (lambda () (values 1 2 3)) list))
(newline)
(display (call-with-values (lambda () (values)) list))
(newline)
min-max returns two values in a single call. The first
consumer is a lambda with two parameters, low and
high. Any procedure can be a consumer: - subtracts
the second value from the first, and list collects all values
into a list. (values) with no arguments returns no values at
all, so list is called without arguments. In the terms of the
continuations article, values
passes several values to its continuation, and
call-with-values sets up a continuation that accepts them.
$ guile first.scm lowest: 1, highest: 9 -8 (1 2 3) ()
receive
call-with-values needs two lambda expressions and
reads backwards. SRFI 8 defines the shorter receive form, which
binds the values to names like a let. In Guile it comes from
the (ice-9 receive) module. Several built-in procedures return
two values as well.
(use-modules (ice-9 receive))
(define (split-name full)
(let ((space (string-index full #\space)))
(values (substring full 0 space)
(substring full (+ space 1)))))
(receive (first last) (split-name "Lucia Novakova")
(display last)
(display ", ")
(display first)
(newline))
(receive (q r) (floor/ 17 5)
(display (list 'floor q r))
(newline))
(receive (q r) (truncate/ -17 5)
(display (list 'truncate q r))
(newline))
(receive (root rest) (exact-integer-sqrt 50)
(display (list 'sqrt root rest))
(newline))
(receive (head . others) (values 'a 'b 'c 'd)
(display (list head others))
(newline))
(receive all (values 1 2 3)
(display all)
(newline))
split-name returns the parts before and after the space, and
receive names them first and last.
floor/ and truncate/ return the quotient and
remainder of the two kinds of integer division described in the
numbers article, and
exact-integer-sqrt returns the integer square root and what is
left over: 7² + 1 = 50. The formals of receive work like the
parameters of a lambda: a dotted list collects the remaining
values in a list, and a single name collects all of them.
$ guile receive.scm Novakova, Lucia (floor 3 2) (truncate -3 -2) (sqrt 7 1) (a (b c d)) (1 2 3)
let-values and let*-values
SRFI 11, provided by the (srfi srfi-11) module, adds
let-values and let*-values, the forms included in
R7RS. They bind the values of several expressions at once, the same way
let and let* bind single values.
(use-modules (srfi srfi-11))
(define (stats numbers)
(let ((n (length numbers))
(sum (apply + numbers)))
(values n sum (/ sum n))))
(let-values (((count total mean) (stats '(4 8 15 16 23 42)))
((q r) (floor/ 100 7)))
(display (list count total mean))
(newline)
(display (list q r))
(newline))
(define (hours-minutes total-minutes)
(floor/ total-minutes 60))
(define (days-hours total-hours)
(floor/ total-hours 24))
(let*-values (((hours minutes) (hours-minutes 4000))
((days hours) (days-hours hours)))
(display (list days 'days hours 'hours minutes 'minutes))
(newline))
(let-values (((first . rest) (values 1 2 3))
(everything (values 4 5)))
(display (list first rest everything))
(newline))
stats returns the count, the sum, and the mean of a list. The
first let-values binds the results of two producers in one form.
As with let, the expressions cannot see each other's variables.
let*-values binds from left to right, so the second clause uses
the hours from the first one and rebinds the name to the hours
left after the days are taken out. The last form shows the dotted and the
single-name formals.
$ guile let-values.scm (6 108 18) (14 2) (2 days 18 hours 40 minutes) (1 (2 3) (4 5))
define-values
define-values defines a variable for each value. It works at the
top level and inside a body, where it can be mixed with other internal
definitions. Guile provides it without an import.
(define-values (width height) (values 1920 1080)) (display (list width height)) (newline) (define (aspect w h) (define-values (q r) (floor/ w h)) (define d (gcd w h)) (list q r (/ w d) (/ h d))) (display (aspect width height)) (newline) (define-values (head . tail) (values 'x 'y 'z)) (display (list head tail)) (newline) (define-values all (values 1 2 3)) (display all) (newline)
The first form defines width and height as
top-level variables. Inside aspect, define-values
takes the result of floor/ and a plain define
follows it; the procedure returns the whole quotient, the remainder, and the
reduced ratio of the screen, 16:9. Dotted and single-name formals work here
too.
$ guile define-values.scm (1920 1080) (1 840 16 9) (x (y z)) (1 2 3)
Values in a single-value context
Most places in a program expect exactly one value: an argument of a
procedure call, the initial value of a variable, a test in an
if. The standard leaves it unspecified what happens when such a
place receives a different number of values. Guile handles the two cases
differently.
(define (two) (values 1 2)) (define (none) (values)) (display (two)) (newline) (display (+ (two) 10)) (newline) (define x (two)) (display x) (newline) (none) (display "zero values in a body: fine") (newline) (display (list 'result (none))) (newline)
When a single value is expected and several arrive, Guile keeps the first one
and drops the others without a warning: (display (two)) prints
1, and x is defined as 1. A body expression whose value is not
used may return any number of values, so the bare (none) call is
fine. Passing zero values as an argument is an error. The output omits the
backtrace.
$ guile context.scm 1 11 1 zero values in a body: fine context.scm:17:23: Zero values returned to single-valued continuation
SRFI 1 procedures with two results
Several procedures of the SRFI 1 list library divide a list into two parts and return both as values. They are a common place to meet multiple values in everyday code.
(use-modules (srfi srfi-1) (ice-9 receive))
(define numbers '(3 8 1 9 4 6 7 2))
(receive (evens odds) (partition even? numbers)
(display (list 'evens evens 'odds odds))
(newline))
(receive (front back) (split-at numbers 5)
(display (list front back))
(newline))
(receive (small rest) (span (lambda (n) (< n 9)) numbers)
(display (list small rest))
(newline))
(receive (before after) (break even? numbers)
(display (list before after))
(newline))
(define people '(("Eva" 31) ("Tomas" 45) ("Jana" 29)))
(receive (names ages) (unzip2 people)
(display names)
(newline)
(display (list 'average (/ (apply + ages) (length ages))))
(newline))
partition separates the elements that satisfy a predicate from
those that do not, in one pass. split-at cuts the list after a
number of elements. span takes the longest prefix whose elements
satisfy the predicate, and break takes the longest prefix whose
elements do not. unzip2 turns a list of two-element lists into
two lists, here the names and the ages.
$ guile srfi1.scm (evens (8 4 6 2) odds (3 1 9 7)) ((3 8 1 9 4) (6 7 2)) ((3 8 1) (9 4 6 7 2)) ((3) (8 1 9 4 6 7 2)) (Eva Tomas Jana) (average 35)
Values and lists
Multiple values are not a data structure: they cannot be stored in a variable
or put into a list as they are. When they must be kept, they are converted to
a list, and apply with values converts a list back.
(use-modules (ice-9 receive))
(define (values->list producer)
(call-with-values producer list))
(define (list->values xs)
(apply values xs))
(display (values->list (lambda () (floor/ 47 10))))
(newline)
(receive (a b c) (list->values '(x y z))
(display (list c b a))
(newline))
(display (call-with-values (lambda () (list->values '(1 2 3 4))) +))
(newline)
(define (time-parts seconds)
(receive (minutes secs) (floor/ seconds 60)
(receive (hours mins) (floor/ minutes 60)
(values hours mins secs))))
(display (map time-parts '(59 3600 7384)))
(newline)
(display (map (lambda (s) (values->list (lambda () (time-parts s))))
'(59 3600 7384)))
(newline)
values->list collects the values of a producer, and
list->values spreads a list into values. Values from
list->values can be passed to any procedure, such as
+. time-parts splits seconds into hours, minutes,
and seconds with two nested receive forms. map
expects one value from each call, so mapping time-parts directly
keeps only the hours. Converting the values to a list inside the
lambda keeps all three. As a rule, values suit a small, fixed
number of results that the caller uses right away, while a list suits results
that are stored or whose number varies.
$ guile lists.scm (4 7) (z y x) 10 (0 1 2) ((0 0 59) (1 0 0) (2 3 4))
A parser that returns the rest
Parsers are a classic use of multiple values. Each parsing procedure consumes some tokens from the front of the input and returns two values: what it has built and the tokens it has not used. The example evaluates arithmetic written in infix notation as a Scheme list, with sublists for parentheses.
(use-modules (ice-9 receive))
(define (parse-factor tokens)
(let ((token (car tokens)))
(if (pair? token)
(receive (value rest) (parse-sum token)
(values value (cdr tokens)))
(values token (cdr tokens)))))
(define (parse-product tokens)
(receive (left rest) (parse-factor tokens)
(let loop ((acc left) (rest rest))
(if (and (pair? rest) (memq (car rest) '(* /)))
(receive (right more) (parse-factor (cdr rest))
(loop (if (eq? (car rest) '*) (* acc right) (/ acc right))
more))
(values acc rest)))))
(define (parse-sum tokens)
(receive (left rest) (parse-product tokens)
(let loop ((acc left) (rest rest))
(if (and (pair? rest) (memq (car rest) '(+ -)))
(receive (right more) (parse-product (cdr rest))
(loop (if (eq? (car rest) '+) (+ acc right) (- acc right))
more))
(values acc rest)))))
(define (calculate expr)
(receive (value rest) (parse-sum expr)
(if (null? rest)
value
(list 'unexpected rest))))
(for-each (lambda (expr)
(write expr)
(display " = ")
(write (calculate expr))
(newline))
'((2 + 3 * 4)
((1 + 2) * (3 + 4))
(10 - 4 - 3)
(7 / 2 + 1)
(2 * (3 + (4 - 1)) / 4)
(2 * 3 4)))
parse-factor reads a number, or a parenthesized sublist, which
it parses as a whole sum. parse-product reads a factor and then
keeps reading * or / followed by another factor,
and parse-sum does the same with + and
- on top of products. This gives multiplication a higher
precedence than addition, and the loops evaluate operators of the same level
from left to right. Every procedure passes the unused tokens back to its
caller, so no shared position variable is needed. calculate
reports the tokens that are left after a complete sum.
$ guile parser.scm (2 + 3 * 4) = 14 ((1 + 2) * (3 + 4)) = 21 (10 - 4 - 3) = 3 (7 / 2 + 1) = 9/2 (2 * (3 + (4 - 1)) / 4) = 3 (2 * 3 4) = (unexpected (4))
Source
Returning and Accepting Multiple Values — GNU Guile Reference Manual
In this article we have returned several values with values,
consumed them with call-with-values, receive,
let-values, let*-values, and
define-values, used the built-in procedures and the SRFI 1
procedures that return two values, seen how Guile treats values in a
single-value context, converted between values and lists, and written a
parser that returns its result together with the remaining input. The
procedures article covers the parameter
lists that the forms here share, and the
continuations article explains the
mechanism that carries the values.
Author
List all Scheme tutorials.