Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple return values of floor in dotimes

The floor Hyperspec article on dotimes has this example:

(defun palindromep (string &optional
                           (start 0)
                           (end (length string)))
   (dotimes (k (floor (- end start) 2) t)
    (unless (char-equal (char string (+ start k))
                        (char string (- end k 1)))
      (return nil))))

If floor returns two values, e.g. (floor 5 2) -> 2 and 1, how does dotimes know to just use the first value and disregard the second for its count-form?

like image 417
147pm Avatar asked Jan 28 '26 13:01

147pm


1 Answers

It's a general mechanism and not specific to dotimes.

If one calls a function or sets a variable, then only the first value will be passed:

CL-USER 52 > (defun foo (x) x)
FOO

CL-USER 53 > (foo (floor 5 2))
2

CL-USER 54 > (let ((foo (floor 5 2)))
               foo)
2

To do the equivalent (calling functions, binding variables) with multiple values, one needs to use special constructs:

CL-USER 55 > (multiple-value-call #'list
               (floor 5 2) (floor 7 3)) 
(2 1 2 1)

CL-USER 56 > (multiple-value-bind (foo0 foo1)
                 (floor 5 2)
               (list foo0 foo1))
(2 1)
like image 145
Rainer Joswig Avatar answered Feb 01 '26 01:02

Rainer Joswig