From: Peter Seibel
Subject: returning multiple values vs a list
Date: 
Message-ID: <m3is8vk66b.fsf@javamonkey.com>
Suppose I'm writing a function to take apart a list into a few pieces.
I can write it to return multiple values and let callers use
MULTIPLE-VALUE-BIND to grab the individual parts. Or I can write it to
return a list and let callers use DESTRUCTURING-BIND. Any reason to
prefer one or the other? Suppose for the sake of argument that
everywhere I use it I use all the values; obviously if I sometimes
wanted only the primary value then multiple values would be the way to
go.

-Peter


-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp

From: Paul F. Dietz
Subject: Re: returning multiple values vs a list
Date: 
Message-ID: <41804DB7.2090308@dls.net>
Peter Seibel wrote:
> Suppose I'm writing a function to take apart a list into a few pieces.
> I can write it to return multiple values and let callers use
> MULTIPLE-VALUE-BIND to grab the individual parts. Or I can write it to
> return a list and let callers use DESTRUCTURING-BIND. Any reason to
> prefer one or the other? Suppose for the sake of argument that
> everywhere I use it I use all the values; obviously if I sometimes
> wanted only the primary value then multiple values would be the way to
> go.

Multiple values can be implemented without heap allocation;
that's harder to do for a list.

Another thing you could do is allocate a simple vector in the caller,
pass the simple vector to the function, and have the function fill in
the vector.  If the vector is declared dynamic extent it can be heap
allocated.  Wrap with suitable macros to hide the details.

	Paul
From: Peter Seibel
Subject: Re: returning multiple values vs a list
Date: 
Message-ID: <m3ekjjk2tr.fsf@javamonkey.com>
"Paul F. Dietz" <·····@dls.net> writes:

> If the vector is declared dynamic extent it can be heap allocated.

You mean stack allocated, right?

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Paul F. Dietz
Subject: Re: returning multiple values vs a list
Date: 
Message-ID: <YtydnaLmxv6GwB3cRVn-qQ@dls.net>
Peter Seibel wrote:

> You mean stack allocated, right?

Yes.

	Paul
From: Adam Warner
Subject: Re: returning multiple values vs a list
Date: 
Message-ID: <pan.2004.10.28.02.32.46.574554@consulting.net.nz>
Hi Paul F. Dietz,

>> Suppose I'm writing a function to take apart a list into a few pieces.
>> I can write it to return multiple values and let callers use
>> MULTIPLE-VALUE-BIND to grab the individual parts. Or I can write it to
>> return a list and let callers use DESTRUCTURING-BIND. Any reason to
>> prefer one or the other? Suppose for the sake of argument that
>> everywhere I use it I use all the values; obviously if I sometimes
>> wanted only the primary value then multiple values would be the way to
>> go.
> 
> Multiple values can be implemented without heap allocation;
> that's harder to do for a list.

To provide an example of how much more efficient this can be in practice,
here's a quotation from the CMUCL User's Manual:

   5.4.6 Multiple Values Optimization

   Within a function, Python implements uses of multiple values
   particularly efficiently. Multiple values can be kept in arbitrary
   registers, so using multiple values doesn't imply stack manipulation
   and representation conversion. For example, this code:

   (let ((a (if x (foo x) u))
         (b (if x (bar x) v)))
     ...)

   is actually more efficient written this way:

   (multiple-value-bind
       (a b)
       (if x
           (values (foo x) (bar x))
           (values u v))
     ...)

   Also, see section 5.6.5 for information on how local call provides
   efficient support for multiple function return values.

The problem with MULTIPLE-VALUE-BIND is that its name is too long for such
an important low cost operator. How awkward would CAR be if its shortest
name was LISTS-FIRST-ELEMENT?

Regards,
Adam
From: Hannah Schroeter
Subject: Re: returning multiple values vs a list
Date: 
Message-ID: <clqqum$bn0$1@c3po.use.schlund.de>
Hello!

Adam Warner  <······@consulting.net.nz> wrote:

>The problem with MULTIPLE-VALUE-BIND is that its name is too long for such
>an important low cost operator. How awkward would CAR be if its shortest
>name was LISTS-FIRST-ELEMENT?

(defpackage "MY-CL")
(in-package "MY-CL")

(defmacro mvb (&rest foo) `(multiple-value-bind ,@foo))

;;

(defpackage "WHATEVER" (:use "MY-CL"))
(in-package "WHATEVER")

(defun foo ()
  (mvb (a b) (floor 5 2)
    ...))

Kind regards,

Hannah.
From: Marco Baringer
Subject: Re: returning multiple values vs a list
Date: 
Message-ID: <m2lldrvujx.fsf@bese.it>
······@schlund.de (Hannah Schroeter) writes:

> Adam Warner  <······@consulting.net.nz> wrote:
>
>>The problem with MULTIPLE-VALUE-BIND is that its name is too long for such
>>an important low cost operator. How awkward would CAR be if its shortest
>>name was LISTS-FIRST-ELEMENT?
>
> (defpackage "MY-CL")
> (in-package "MY-CL")
>
> (defmacro mvb (&rest foo) `(multiple-value-bind ,@foo))
>
> ;;
>
> (defpackage "WHATEVER" (:use "MY-CL"))
> (in-package "WHATEVER")
>
> (defun foo ()
>   (mvb (a b) (floor 5 2)
>     ...))

or even:

(mvb C-TAB RET
=>
(multiple-value-bind

-- 
-Marco
Ring the bells that still can ring.
Forget your perfect offering.
There is a crack in everything.
That's how the light gets in.
     -Leonard Cohen
From: Rahul Jain
Subject: Re: returning multiple values vs a list
Date: 
Message-ID: <87r7nfabty.fsf@nyct.net>
Adam Warner <······@consulting.net.nz> writes:

> The problem with MULTIPLE-VALUE-BIND is that its name is too long for such
> an important low cost operator. How awkward would CAR be if its shortest
> name was LISTS-FIRST-ELEMENT?

m-v-b<M-tab>

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: Brian Downing
Subject: Re: returning multiple values vs a list
Date: 
Message-ID: <Mrphd.340667$3l3.129580@attbi_s03>
In article <······························@consulting.net.nz>,
Adam Warner  <······@consulting.net.nz> wrote:
> The problem with MULTIPLE-VALUE-BIND is that its name is too long for
> such an important low cost operator. How awkward would CAR be if its
> shortest name was LISTS-FIRST-ELEMENT?

Just `mvb' will do, if you bind something convenient to
slime-fuzzy-complete-symbol (defaults to C-c M-i).

Other examples (best match first):

prpr    => *print-pretty*
           *print-pprint-dispatch*
norm-df => least-positive-normalized-double-float
           least-negative-normalized-double-float
desb    => destructuring-bind
           describe
           define-symbol-macro
dbind   => destructuring-bind
           handler-bind
stdin   => *standard-input*
           set-difference

You can also change `slime-complete-symbol-function' to use this all the
time, but I think it's better as a complement to the usual Emacs-style
completion, not a replacement.

(However, I tend to agree with you - MULTIPLE-VALUE-BIND is a bit
lengthy for what it does.)

-bcd
-- 
*** Brian Downing <bdowning at lavos dot net> 
From: Kaz Kylheku
Subject: Re: returning multiple values vs a list
Date: 
Message-ID: <cf333042.0410272321.3e411759@posting.google.com>
Peter Seibel <·····@javamonkey.com> wrote in message news:<··············@javamonkey.com>...
> Suppose I'm writing a function to take apart a list into a few pieces.
> I can write it to return multiple values and let callers use
> MULTIPLE-VALUE-BIND to grab the individual parts. Or I can write it to
> return a list and let callers use DESTRUCTURING-BIND. Any reason to
> prefer one or the other?

One reason for M-V-BIND is that if there are fewer values returned
than it asks for, it's tolerant in the sense that it pads the extra
variables with NIL values. D-BIND will signal an error.

On the other hand, multiple values can be inconvenient because you
must deal with them explicitly if you want them, even if you just want
to pass them elsewhere, like as arguments to another function call.

If you already have some existing functional plumbing that deals with
these lists, it's easer to just play along and return the list.

In particular, if you find yourself doing MULTIPLE-VALUE-LIST a great
deal, you are probably not taking full advantage of the properties
multiple values anyway; in that situation they are screaming that they
want to be a list. ;)