[plt-scheme] Multiple values
On Mon, 2006-02-20 at 14:10 -0800, Gregory Woodhouse wrote:
. . .
> > So, if you were to think of this slightly
> > more generally, you might compare this:
> >
> > (let ([x (compute-x ...)]
> > [y (compute-y ...)])
> > (+ x y))
> >
> > to this:
> >
> > (let-values ([(x y) (compute-both-x-and-y ...)])
> > (+ x y))
> >
> > hth,
> > Robby
>
> Yes, that makes sense. Thanks.
One of my favorite examples is the Common Lisp floor function:
(floor x y) returns (values (quotient x y) (modulo x y)),
with quotient and modulo as defined in Scheme; some languages provide a
divmod function returning the same values in a tuple. I've never been
entirely sure whether there is any genuine efficiency gain of the single
function over separate calls to the two component functions, but I've
always viewed the single function as somehow more elegant and
mathematical -- you know, direct provision of the Euclidean Algorithm
and all that.
-- Bill Wood