我想分配给两个(或多个)变量,例如x
和y
(z
等)从用户输入读取的相同值。现在我有了(setf x (read))
,但是我也想输入值y
。我是否必须这样做(setf y x)
?还有更优雅的东西吗?
(setf x (read) y (read))
只能让用户输入两次,所以这是不好的。
尽管您可以轻松实现它,但没有什么比该语言内置的功能更好。例如,这是一个宏setf *(尽管不是最佳名称),它为一堆地方(不必是变量)分配了一个值:
(defmacro setf* ((&rest places) value)
(let ((temp (gensym)))
`(let ((,temp ,value))
(setf ,@(mapcan (lambda (place)
(list place temp))
places)))))
CL-USER> (macroexpand-1 '(setf* (a (car list) (aref array 2 3)) d))
(LET ((#:G1043 D))
(SETF A #:G1043
(CAR LIST) #:G1043
(AREF ARRAY 2 3) #:G1043))
你会的
(setf* (x y) (read))
当然,对于简单的关闭,您也可以手动执行此操作(如sds所示):
(setf x (read)
y x)
或者
(let ((temp (read)))
(setf x temp
y temp))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句