Hi All,
This is an old issue, but as far as I know, there is no convenient to
discard extra values in a context where multiple values are received.
I propose we add first-value, second-value, and, nth-value:
> (first-value (values 'a 'b 'c))
'a
> (second-value (values 'a 'b 'c 'd))
'b
> (nth-value (values 'a 'b 'c 'd) 2)
'c
The most common use-case for me is discarding extra values from for/fold.
These operations could be implemented like this:
(define-syntax-rule (first-value expr)
(call-with-values (λ () expr) (λ (a . _) a)))
(define-syntax-rule (second-value expr)
(call-with-values (λ () expr) (λ (a b . _) b)))
(define-syntax-rule (nth-value expr n)
(call-with-values (λ () expr)
(λ vs
(unless (>= (length vs) n)
(error 'nth-value (~a "expected at least " n "values")))
(list-ref vs n))))
However thinking about the efficiency - it feels wrong to use call-with-values.
Does the optimizer handle call-with-values so well, that first-value and second-value becomes efficient?
/Jens Axel