tags:

views:

131

answers:

2

I have read documentation for functions such as values and define-values that return and consume multiple values. I understand what they do. It's not clear to me when you would want to use such a thing.

When would it be bad/impossible to build a single list of values and consume that single list of values instead?

+3  A: 

define-values is a convenience that lets you directly bind variables to the results of a expression. It saves you some typing as you don't have to explicitly unpack a list. I don't think there are situations where it is bad or impossible to build a single list of values. In fact, that will be more portable than define-values.

Vijay Mathew
When you use lists like that you would probably de-structure them immediately with a match library.
grettke
@grettke But not all Schemes have a match library :(
Vijay Mathew
@Vijay: Agreed. There are portable match libraries though.
grettke
+2  A: 

Here is my original post on the topic; it is copied below.

In this thread in comp.lang.scheme the means to return multiple values are discussed. There are seemingly 3 solutions in R6RS:

(import (rnrs))

; let-values + values
(define (foo1)
  (values 1 2 3))

(let-values (((a b c) (foo1)))
  (display (list a b c))
  (newline))

; cps
(define (foo2 k)
  (k 1 2 3))

(foo2 (lambda (a b c) 
        (display (list a b c))
        (newline)))

; list
(define (foo3)
  (list 1 2 3))
(let ((result (foo3)))
  (display result)
  (newline))

Per Aziz and Aaron’s point; you should use the approach that communicates the most information to the reader.

grettke