Skip to content

Instantly share code, notes, and snippets.

@zhouqiang-cl
Last active December 4, 2016 10:41
Show Gist options
  • Save zhouqiang-cl/0a1f4d9d1b41c9be4a5bcc291bec3bb9 to your computer and use it in GitHub Desktop.
Save zhouqiang-cl/0a1f4d9d1b41c9be4a5bcc291bec3bb9 to your computer and use it in GitHub Desktop.
common lisp 的 SETF, PSETF
语法
setf {pair}* => result*
psetf {pair}* => nil
pair::= place newvalue
变量和值
place --- 占位符
newvalue --- 新值
描述
setf 对 place 赋值为 newvalue.
psetf 当 pair 数目超过1个时并行对 place 赋值为 newvalue. 他对每一个pair求值,
但求值顺序不确定. 可能产生意想不到的结果
代码
* (setq x (cons 'a 'b) y (list 1 2 3))
(1 2 3)
* (setf (car x) 'x (cadr y) (car x) (cdr x) y)
(1 X 3)
* x
(X 1 X 3)
* y
(1 X 3)
* (setq x (cons 'a 'b) y (list 1 2 3))
(1 2 3)
* (psetf (car x) 'x (cadr y) (car x) (cdr x) y)
NIL
* x
(X 1 A 3)
* y
(1 A 3)
另外:
 在用setq的地方都可以替换成setf, setq 更底层, setf是一个宏,一个可以转换成set去的宏
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment