[plt-scheme] Why multiple values?

From: gregory.woodhouse at sbcglobal.net (gregory.woodhouse at sbcglobal.net)
Date: Sun Dec 24 15:42:50 EST 2006

In the cases like Danny's example, I've usually returned a list, as in

(list (car x) (cdr x))

and, unless I'm misunderstanding you, the obious way to apply a function to a list of values sems to me to be map, as in

(map (lambda (x) (* x x)) (list 1 2 3))

On the other hand, if I want to do something similar with values, I'd have to write

(call-with-values () (values 1 2 3) (lambda (x y z) (list (* x x) (* y y) (* z z))))

and that's what I don't get. Here, I need to replicate the same expression three times.
 
===
Gregory Woodhouse 
 
"Mathematics is the science of patterns."
--Lynn Arthur Steen, 1988


----- Original Message ----
From: Carl Eastlund <cce at ccs.neu.edu>
To: Greg Woodhouse <gregory.woodhouse at sbcglobal.net>
Cc: PLT Scheme <plt-scheme at list.cs.brown.edu>
Sent: Sunday, December 24, 2006 11:14:48 AM
Subject: Re: [plt-scheme] Why multiple values?


On 12/24/06, Greg Woodhouse <gregory.woodhouse at sbcglobal.net> wrote:
> This is undoubtedly a basic question, but I don't think I quite grasp the concept behind (values ...). I know I can write, say
>
> (define-values (x y z) (values 1 2 3))
>
> but I don't understand why this construct is needed. Couldn't I just as easily write
>
> (define x 1)
> (define y 2)
> (define z 3) ?
>
> I kind of suspect values is there because it supports some programming idiom, or because it is needed in conjunction with some other language feature, but I'm not sure what.

Yes, but if you call a function that produces two values, breaking it
up into two functions that each produce one value might require
duplicating computation.  Multiple values helps when the values come
from a single source that can't be split up.

-- 
Carl Eastlund


Posted on the users mailing list.