常见Lisp中的set,setq和setf之间的区别? [英] Difference between `set`, `setq`, and `setf` in Common Lisp?
问题描述
Common Lisp中 set, setq和 setf之间有什么区别?
What is the difference between "set", "setq", and "setf" in Common Lisp?
推荐答案
在Lisp中,没有词法变量-只有动态变量。
没有SETQ或SETF,只有SET函数。
Originally, in Lisp, there were no lexical variables -- only dynamic ones. And there was no SETQ or SETF, just the SET function.
现在写成:
(setf (symbol-value '*foo*) 42)
的写法是:
(set (quote *foo*) 42)
最终缩写为SETQ(用SET引用):
which was eventually abbreviavated to SETQ (SET Quoted):
(setq *foo* 42)
然后出现了词法变量,SETQ出现了
Then lexical variables happened, and SETQ came to be used for assignment to them too -- so it was no longer a simple wrapper around SET.
后来,有人发明了SETF(SET字段)作为通用方法为数据结构分配值,以反映其他语言的l值:
Later, someone invented SETF (SET Field) as a generic way of assigning values to data structures, to mirror the l-values of other languages:
x.car := 42;
将写为
(setf (car x) 42)
对于对称性和通用性,SETF还提供了SETQ的功能。在这一点上,说SETQ是一个低级原语,而SETF是一个高级操作,是正确的。
For symmetry and generality, SETF also provided the functionality of SETQ. At this point it would have been correct to say that SETQ was a Low-level primitive, and SETF a high-level operation.
然后发生了符号宏。为了使符号宏能够透明地工作,人们意识到,如果分配给变量的确是符号宏,则SETQ必须像SETF一样工作:
Then symbol macros happened. So that symbol macros could work transparently, it was realized that SETQ would have to act like SETF if the "variable" being assigned to was really a symbol macro:
(defvar *hidden* (cons 42 42))
(define-symbol-macro foo (car *hidden*))
foo => 42
(setq foo 13)
foo => 13
*hidden* => (13 . 42)
所以我们到了今天:SET和SETQ是萎缩的残骸的方言,并且很可能会从Common Lisp的最终继承者那里引来。
So we arrive in the present day: SET and SETQ are atrophied remains of older dialects, and will probably be booted from eventual successors of Common Lisp.
这篇关于常见Lisp中的set,setq和setf之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!