dotimes 中 floor 的多个 return 值
Multiple return values of floor in dotimes
dotimes
上的 floor
Hyperspec 文章有这个例子:
(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))))
如果floor
returns 两个值,例如(floor 5 2)
-> 2
和 1
,dotimes
如何知道只使用第一个值而忽略第二个值的计数形式?
来自 7.10.1,
Normally multiple values are not used. Special forms are required both to produce multiple values and to receive them. If the caller of a function does not request multiple values, but the called function produces multiple values, then the first value is given to the caller and all others are discarded; if the called function produces zero values, then the caller gets nil
as a value.
除非您专门 做一些事情来处理多个值(例如通过 multiple-value-call
或配备来处理它们的各种宏之一),除了第一个值将被忽略。
这是一种通用机制,并不特定于 dotimes
。
如果调用函数或设置变量,则只会传递第一个值:
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
要对多个值进行等效(调用函数、绑定变量),需要使用特殊结构:
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)
dotimes
上的 floor
Hyperspec 文章有这个例子:
(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))))
如果floor
returns 两个值,例如(floor 5 2)
-> 2
和 1
,dotimes
如何知道只使用第一个值而忽略第二个值的计数形式?
来自 7.10.1,
Normally multiple values are not used. Special forms are required both to produce multiple values and to receive them. If the caller of a function does not request multiple values, but the called function produces multiple values, then the first value is given to the caller and all others are discarded; if the called function produces zero values, then the caller gets
nil
as a value.
除非您专门 做一些事情来处理多个值(例如通过 multiple-value-call
或配备来处理它们的各种宏之一),除了第一个值将被忽略。
这是一种通用机制,并不特定于 dotimes
。
如果调用函数或设置变量,则只会传递第一个值:
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
要对多个值进行等效(调用函数、绑定变量),需要使用特殊结构:
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)