What's the difference between using (values …)
versus (list …)
(or literally '(one two three …)
) to return multiple values from a lambda
(or other implicit progn
)? Does it create some special glue to multiple-value-bind
? Superficially, I can't see any difference and am curious whether it's merely a convention for multiple return values.
- 2Can the downvoter take the time to drop a note on how to improve this question?– GiorgioCommentedJan 5, 2015 at 19:14
- +1 @Giorgio — i know i asked this a bit hastily, however there's not exactly a lot of model Lisp questions (on many sites, not just SE), and i'd appreciate any pointers to help build the community! :]– RubyTuesdayDONOCommentedJan 5, 2015 at 19:38
1 Answer
This question has been already answered on SO: values function in Common Lisp.
Briefly, multiple values is a facility to return many objects without allocating extra memory. E.g. floor
must return two values - quotient and remainder. It can return a list
(or a pair) or it can return two values. In the former case it will have to allocate a cons cell on each call, in the second it will not.
This means that multiple values have certain limitations (one cannot return more than 20 values portably).
- 1I'd be quite happy if you pulled the entirety of your answer here too (and linked back to it). Note also that if you use the link provided in 'share' (
http://stackoverflow.com/a/22796346/850781
) as the link instead, it would allow you to get the Announcer badge if people click through it.– user40980CommentedJan 5, 2015 at 20:20 - 1Note that it's completely implementation dependant how the values are actually stored. It could be registers, or on the stack, or a thread-local vector, or a series of thread-local vectors for different amounts of values, or a top-of-the-stack-allocated vector, or a heap-allocated user-hidden vector type (thus no longer saving you from consing), or a freshly consed list in an interpreter, or a combination of these, or yet something else. I've seen a few of these in actual implementations.– acelentCommentedFeb 21, 2015 at 0:14