[plt-scheme] Multiple values
On Tue, 2006-02-21 at 00:03 +0100, hufflen at lifc.univ-fcomte.fr wrote:
. . .
> Yes. More generally, I often use multiple values because they allow
> multiple computation. For an "actual" example, let us consider a
> non-empty linear list. Let us assume that we have to get, on the one
> hand, the list but its last element, on the other hand, its last
> element. You can write two functions for these two purposes, but they
> would go along the list twice. Multiple values allow to get the two
> results in one pass:
>
> (define (butlast-and-last l)
> ;; "l" is supposed to be non-empty, returns two values: a linear list
> with
> ;; all the elements of "l" but the last one, and the last one.
> (let ((first (car l))
> (rest (cdr l)))
> (if (null? rest)
> (values '() first)
> (call-with-values (lambda () (butlast-and-last rest))
> (lambda (l0 last-0) (values (cons first l0) last-0))))))
I know I'm coming in *way* late to the discussion (particularly given
the presence of multiple values in Common Lisp), but while I find some
elegance in the multiple values mechanism I have to ask, is there a
killer argument for multiple values over an ordinary function returning
a (proper or improper) list? Does the code above win big over, for
example,
(define (butlast-and-last l)
(let ((first (car l))
(rest (cdr l)))
(if (null? rest)
(list* '() first)
(match (butlast-and-last rest)
([l0 . last0] (list* (cons first l0) last0))))))
or is it a matter of taste. For example, can a compiler do something
about the non-tail recursion in M. Hufflen's example?
Not carping, just a question,
-- Bill Wood