常见Lisp中的set,setq和setf之间的区别?

编程入门 行业动态 更新时间:2024-10-26 19:29:21
本文介绍了常见Lisp中的set,setq和setf之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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之间的区别?

本文发布于:2023-10-29 02:51:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   常见   Lisp   set   setq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!