Is the values function in Common Lisp just syntactic sugar for packaging multiple values into a list that gets destructured by the caller?. I am asking because I thought Common Lisp supports "true" multiple value return rather than returning a tuple or a list as in other languages, such as python. Someone just told me that it's just syntactic sugar, so I would like someone to kindly explain it. To try to understand the type that is returned by the values function, I typed (type-of (values 1 2 3))
, and the output was BIT
. I searched in the Common Lisp reference for that and I couldn't find it mentioned in the datatypes section. Also, can anyone share some resources that suggest how the values function is implemented in Common Lisp?. Thank you.
相关问题
- Generating powerset in one function, no explicit r
- Drakma and Dexador both fails at USocket call whil
- Forming Lisp code to task — related to flatten lis
- clisp 2.49: asdf cannot find asd
- unfold function in scheme
相关文章
- Does learning one Lisp help in learning the other?
- Common Lisp: Why does my tail-recursive function c
- What is the definition of “natural recursion”?
- How do I write a macro-defining macro in common li
- How can I unintern a qualified method?
- Changing the nth element of a list
- Is a “transparent” macrolet possible?
- sleep in emacs lisp
Multiple Values in CL
The language Common lisp is described in the ANSI standard INCITS 226-1994 (R2004) and has many implementations. Each can implement multiple values as it sees fit, and they are allowed, of course, to cons up a list for them (in fact, the Emacs Lisp compatibility layer for CL does just that - but it is, emphatically and intentionally, not a Common Lisp implementation).
Purpose
However, the intent of this facility is to enable passing (at least some) multiple values without consing (i.e., without allocating heap memory) and all CL implementations I know of do that. In this sense the multiple values facility is an optimization.
Of course, the implementation of this feature can be very different for different platforms and scenarios. E.g., the first few (say, 20 - required by the standard) are stored in a static of thread-local vector, the next several (1000?) are allocated on the stack, and the rest (if needed) are allocated on the heap as a vector or list.
Usage
E.g., the function
floor
returns two values. If you writeyou capture only the first one and discard the second one, you need to write
to capture both values. This is similar to what other languages might express as
using tuples, except that CL does not allocate memory to pass (just a few) multiple values, and the other languages often do.
IOW, one can think of multiple values as an ephemeral struct.
Note that CL can convert multiple values to lists:
instead of the more efficient and concise
MV & type
CL does not have a special type for the "multiple value object" exactly because it does not allocate a separate object to pass around multiple values. In this sense one can, indeed, claim that
values
is syntactic sugar.However, in CL one can declare a function type returning multiple values:
This means that
floor
returns two values, bothreal
s (as opposed to returning a value of type(values real real)
), i.e., in this case one might claim abuse of notation.Your case
In your specific case,
type-of
is an ordinary function (i.e., not a macro or special operator). You pass it a single object, 1, because, unless you are usingmultiple-value-bind
and friends, only the first value is used, sois identical to
and type of 1 is
bit
.PS: Control return values
One use of
values
is to control the return values of a function. Normally a CL function's return values are those of the last form. Sometimes it is not desirable, e.g., the last form return multiple values and you want your function to return one value (or none, likevoid
inC
):If you make a CL implementation you could implement it with lists as long as it coheres to the spec. You need to handle one value specific and you need some way to tag zero, 2..n values and the other functions need to understand that format and print can be made to display it the same way as in other makes.
Most likely
values
and its sister functions is an optimization where the implementations use the stack instead of consing the values to a list structure just to get it destructured in the next level. In the olden times where RAM and CPU was not to be wasted it was very important, but I doubt you'll notice real trouble should you usedestructuring-bind
instead ofmultiple-value-bind
today.Common Lisp differs from Scheme a great deal in the positive direction that you can make a function, eg.
floor
where in it's calculations end up with the remainder in addition to the quotient answer, return all values at the same time but you are allowed to use it as if it only returned the very first value. I really miss that sometimes when writing Scheme since it demands you have acall-with-values
that is similar tomultiple-value-call
or syntactic sugar likelet-values
to handle all the returned values that again makes you end up with making three versions in case you only need just one of the values.The
values
function isn't just syntactic sugar for making a list for the caller to destructure.For one, if the caller expects only a single value, it will get only one value (the first), not a list, from a form that returns multiple values. Since
type-of
takes only one value as an argument, it is giving you the type of the first value, 1. 1 is of type BIT.Each Common Lisp implementation is free to pursue its own strategy for implementing multiple values. I learned a lot from what Frode Fjeld wrote about how his implementation, Movitz, handles it in The Movitz development platform, section 2.5.