jakHunter
jakHunter

Reputation: 315

How can you return the last value in common lisp

I know that the values function helps control REPL but I want to explicitly return the last value from a function. The only thing close to that I managed to find to what i was looking for is

 (setf (values q a) (floor 1.5))

But I am only interested in the 0.5 that it returns. My question is how can I only return the last value of a function not just the second but the n-th value(whichever it maybe)?

Upvotes: 2

Views: 470

Answers (1)

jkiiski
jkiiski

Reputation: 8411

There is no standard operator to get the last value, or even to iterate over values, but you can work around that using MULTIPLE-VALUE-CALL:

(defun last-arg (&rest args)
  (first (last args)))

(defmacro multiple-value-last (form)
  `(multiple-value-call #'last-arg ,form))

(multiple-value-last (floor 1.5))
; => 0.5
(multiple-value-last (values 10 20 30))
; => 30

At least on SBCL the standard MULTIPLE-VALUE-LIST seems to be implemented this way.

Upvotes: 5

Related Questions