-1

What happens in the following common-lisp Code with number 2 and 3:

"(values (values 1 2 3) 4 5)"

Output is: 1 4 5.

Someone in another forum explain to me, that "the values 2 and 3 are effectively discarded because the outer "(values ...)" call, only cares about first value of the inner "(values 1 2 3)" call.

My question is, why does this happen?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Filipmk
  • 11

1 Answers1

2

values is a function: in particular it is not a special operator. For a function named f in CL then (f v1 v2 ...):

  • evaluates each of the v1, v2, ... in order, collecting a single value from each one, either the first value, or if there are no values then nil;
  • calls f with these values.

This explains why (values (values ...)) selects only the first value from the inner values call.

If you want to collect all the values you need a special form where the normal function-call semantics are not followed. CL provides a special operator which lets you write such forms: multiple-value-call, and the trick is that the function argument in the special form should be values itself:

> (multiple-value-call #'values (values 1 2 3) 4 5)
1
2
3
4
5
* 
ignis volens
  • 7,040
  • 2
  • 12