[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

question about returning multiple values

Jim Newton

5/20/2016 8:44:00 AM

If some function returns multiple values, I can get the values by using
multiple-value-list. E.g., (multiple-values (values 1 2 3)) --> (1 2 3)

But how can I do the opposite? I.e., given a list such as (1 2 3)
how can I return its contents as multiple values?
Apply seems to work, but I'm surprised it does. Is there a more correct way?
(apply #'values '(1 2 3))
--> 1, 2, 3

11 Answers

Carlos

5/20/2016 10:24:00 AM

0

On 20/05/2016 10:44, Jim Newton wrote:
> If some function returns multiple values, I can get the values by using
> multiple-value-list. E.g., (multiple-values (values 1 2 3)) --> (1 2 3)
>
> But how can I do the opposite? I.e., given a list such as (1 2 3)
> how can I return its contents as multiple values?
> Apply seems to work, but I'm surprised it does. Is there a more correct way?
> (apply #'values '(1 2 3))
> --> 1, 2, 3
>

Use VALUES-LIST.

--

Jim Newton

5/20/2016 3:17:00 PM

0

cool thanks.

> Use VALUES-LIST.
>

BTW, I'm really surprised that (apply #'values '(1 2 3)) works.

rpw3

5/20/2016 4:07:00 PM

0

Jim Newton <jimka.issy@gmail.com> wrote:
+---------------
| > Use VALUES-LIST.
|
| BTW, I'm really surprised that (apply #'values '(1 2 3)) works.
+---------------

VALUES is just an ordinary[1] function, not a macro or
a special form. As long as the list given to it is less
than CALL-ARGUMENTS-LIMIT long, why *shouldn't* it work?!?


-Rob

[1] Albeit almost certainly a primitive builtin function.
[I know of no obviouis way to metacircularly define VALUES
within Common Lisp itself.]


-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <http://rpw...
San Mateo, CA 94403

Jim Newton

5/20/2016 4:20:00 PM

0

On Friday, May 20, 2016 at 6:15:06 PM UTC+2, Rob Warnock wrote:

>
> VALUES is just an ordinary[1] function, not a macro or
> a special form. As long as the list given to it is less
> than CALL-ARGUMENTS-LIMIT long, why *shouldn't* it work?!?
>
>


Well I though VALUES was the special form which actually triggered multiple values.

Didier Verna

5/20/2016 4:35:00 PM

0

rpw3@rpw3.org (Rob Warnock) wrote:

> [1] Albeit almost certainly a primitive builtin function. [I know of
> no obviouis way to metacircularly define VALUES within Common Lisp
> itself.]

Except maybe using dynamic-extent somewhere ?

--
Resistance is futile. You will be jazzimilated.

Lisp, Jazz, Aïkido: http://www.didier...

Pascal J. Bourguignon

5/20/2016 6:49:00 PM

0

rpw3@rpw3.org (Rob Warnock) writes:

> Jim Newton <jimka.issy@gmail.com> wrote:
> +---------------
> | > Use VALUES-LIST.
> |
> | BTW, I'm really surprised that (apply #'values '(1 2 3)) works.
> +---------------
>
> VALUES is just an ordinary[1] function, not a macro or
> a special form. As long as the list given to it is less
> than CALL-ARGUMENTS-LIMIT long, why *shouldn't* it work?!?
>
>
> -Rob
>
> [1] Albeit almost certainly a primitive builtin function.
> [I know of no obviouis way to metacircularly define VALUES
> within Common Lisp itself.]

Well multiple values are not specified as first class objects, but
nothing prevents an implementation to reify them.

(defstruct multiple-values
(length 0 :type (integer 0))
first
(rest nil :type list))

(defmethod print-object ((self multiple-values) stream)
(if (zerop (multiple-values-length self))
(format stream "; No values.~%")
(format stream "~@[--> ~{~A~% ~}~]"
(cons (multiple-values-first self)
(multiple-values-rest self)))))

(shadow 'values)
(defun values (&rest values)
(make-multiple-values :length (length values)
:first (first values)
:rest (rest values)))


cl-user> (values 1 2 3)
--> 1
2
3

cl-user> (values)
; No values.

cl-user>


Of course, you will have to re-implement apply and funcall too, so that
when a function returns a multiple-values structure, only
(multiple-values-first result) is passed as argument to the next
function.



--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Kaz Kylheku

5/20/2016 7:39:00 PM

0

On 2016-05-20, Jim Newton <jimka.issy@gmail.com> wrote:
> On Friday, May 20, 2016 at 6:15:06 PM UTC+2, Rob Warnock wrote:
>
>>
>> VALUES is just an ordinary[1] function, not a macro or
>> a special form. As long as the list given to it is less
>> than CALL-ARGUMENTS-LIMIT long, why *shouldn't* it work?!?
>>
>>
>
>
> Well I though VALUES was the special form which actually triggered multiple values.

Internally, the values function has to rely on some such a thing, of
course:

(defun values (&rest args)
(sys:somehow-construct-multiple-values-on-stack args))

A decent Lisp compiler has to inline this; you don't want to be
actually branching to some other function just to establish the return
values of your own (or that it doesn't have any).

Kaz Kylheku

5/20/2016 8:23:00 PM

0

On 2016-05-20, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> rpw3@rpw3.org (Rob Warnock) writes:
>
>> Jim Newton <jimka.issy@gmail.com> wrote:
>> +---------------
>> | > Use VALUES-LIST.
>> |
>> | BTW, I'm really surprised that (apply #'values '(1 2 3)) works.
>> +---------------
>>
>> VALUES is just an ordinary[1] function, not a macro or
>> a special form. As long as the list given to it is less
>> than CALL-ARGUMENTS-LIMIT long, why *shouldn't* it work?!?
>>
>>
>> -Rob
>>
>> [1] Albeit almost certainly a primitive builtin function.
>> [I know of no obviouis way to metacircularly define VALUES
>> within Common Lisp itself.]
>
> Well multiple values are not specified as first class objects, but
> nothing prevents an implementation to reify them.

Yes, something does prevent that.

> Of course, you will have to re-implement apply and funcall too, so that
> when a function returns a multiple-values structure, only
> (multiple-values-first result) is passed as argument to the next
> function.

Hence, it is not a first-class object, and thus not properly re-ified;
a first-class object doesn't vaporize down to some constituent value
when passed to a function.

The multiple-value object can exist in a re-ified "gestational form"
internally, but then has to flip to this second-class
object when it is ready to be "born" into the application.

Carlos

5/20/2016 8:35:00 PM

0

[rpw3@rpw3.org (Rob Warnock), 2016-05-20 16:06]
> Jim Newton <jimka.issy@gmail.com> wrote:
> +---------------
> | > Use VALUES-LIST.
> |
> | BTW, I'm really surprised that (apply #'values '(1 2 3)) works.
> +---------------
>
> VALUES is just an ordinary[1] function, not a macro or
> a special form. As long as the list given to it is less
> than CALL-ARGUMENTS-LIMIT long, why *shouldn't* it work?!?
>
>
> -Rob
>
> [1] Albeit almost certainly a primitive builtin function.
> [I know of no obviouis way to metacircularly define VALUES
> within Common Lisp itself.]

Use VALUES-LIST.

;)
--

Barry Margolin

5/20/2016 10:32:00 PM

0

In article <20160520123003.235@kylheku.com>,
Kaz Kylheku <545-066-4921@kylheku.com> wrote:

> On 2016-05-20, Jim Newton <jimka.issy@gmail.com> wrote:
> > On Friday, May 20, 2016 at 6:15:06 PM UTC+2, Rob Warnock wrote:
> >
> >>
> >> VALUES is just an ordinary[1] function, not a macro or
> >> a special form. As long as the list given to it is less
> >> than CALL-ARGUMENTS-LIMIT long, why *shouldn't* it work?!?
> >>
> >>
> >
> >
> > Well I though VALUES was the special form which actually triggered multiple
> > values.
>
> Internally, the values function has to rely on some such a thing, of
> course:
>
> (defun values (&rest args)
> (sys:somehow-construct-multiple-values-on-stack args))
>
> A decent Lisp compiler has to inline this; you don't want to be
> actually branching to some other function just to establish the return
> values of your own (or that it doesn't have any).

It might even be something like this:

(defun values (&rest args)
(values-list args))

(defun values-list (values)
(values-list values))

The second function looks like infinite recursion, but it's not because
the compiler open-codes the inner call.

I remember once looking at the source code for a Symbolics Lisp Machine,
it's full of code like that:

(defun cons (car cdr)
(cons car cdr))
(defun car (cons)
(car cons))
(defun cdr (cons)
(cdr cons))

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***