1

In Practical Common Lisp, Peter Seibel write:

The mechanism by which multiple values are returned is implementation dependent just like the mechanism for passing arguments into functions is. Almost all language constructs that return the value of some subform will "pass through" multiple values, returning all the values returned by the subform. Thus, a function that returns the result of calling VALUES or VALUES-LIST will itself return multiple values--and so will another function whose result comes from calling the first function. And so on.

The implementation dependent does worry me. My understanding is that the following code might just return primary value:

> (defun f ()
    (values 'a 'b))

> (defun g ()
    (f))

> (g) ; ==> a ? or a b ?

If so, does it mean that I should use this feature sparingly?

Any help is appreciated.

shynur
  • 334
  • 10
  • See also [values function in Common Lisp](https://stackoverflow.com/q/22795608/850781). – sds Oct 21 '22 at 12:41

2 Answers2

7

It's implementation-dependent in the sense that how multiple values are returned at the CPU level may vary from implementation to implementation. However, the semantics are well-specified at the language level and you generally do not need to be concerned about the low-level implementation.

See section 2.5, "Function result protocol", of The Movitz development platform for an example of how one implementation handles multiple return values:

The CPU’s carry flag (i.e. the CF bit in the eflags register) is used to signal whether anything other than precisely one value is being returned. Whenever CF is set, ecx holds the number of values returned. When CF is cleared, a single value in eax is implied. A function’s primary value is always returned in eax. That is, even when zero values are returned, eax is loaded with nil.

It's this kind of low-level detail that may vary from implementation to implementation.

Xach
  • 11,774
  • 37
  • 38
  • Thanks! Moreover, "Almost all language constructs" indicates there are some constructs returning only primary value. These constructs don't include functions, right? (I just want to confirm, and I know why some macros sometimes only return primary value – shynur Oct 21 '22 at 12:02
  • An example of a construct returning only the primary value is `prog1` -- if you wanted to return multiple values, you would have to use `multiple-value-prog1`. "Functions" return multiple values insofar as their last form -- or `return(-from)` -- returns multiple values. Moreover, macros don't return multiple values: they can *generate* code that does so, but they, themselves, can't. – Numbra Oct 21 '22 at 16:47
5

One thing to be aware: there is a limit for the number of values which can be returned on a specific Common Lisp implementation.

The variable MULTIPLE-VALUES-LIMIT has the implementation/machine specific value of the maximum numbers of values which can be returned. The standard says that it should not be smaller than 20. SBCL has a very large number on my computer, while LispWorks has only 51, ECL has 64 and CLISP has 128.

But I can't remember seeing Lisp code which wants to return more than 5 values.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346