Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a common lisp function that returns multiple values

Tags:

common-lisp

I'm doing a tut on lisp http://common-lisp.net/language.html#sec-1 and am wondering how would this function be written:

(my-floor 1.3) 
 => 1 0.3
like image 984
zcaudate Avatar asked Sep 14 '25 11:09

zcaudate


1 Answers

Use values:

(defun foo (x y)
  (values x y (+ x y) (cons x y)))

Try the function:

> (foo 2 pi)
2 ;
3.1415926535897932385L0 ;
5.1415926535897932383L0 ;
(2 . 3.1415926535897932385L0)

Use the returned values with multiple-value-bind:

(multiple-value-bind (a b sum pair) (foo 1 2)
  (list a b sum pair))
==> (1 2 3 (1 . 2))

or (setf values).

See also values function in Common Lisp.

like image 186
sds Avatar answered Sep 17 '25 18:09

sds