Lisp中的setq和defvar [英] setq and defvar in Lisp

查看:370
本文介绍了Lisp中的setq和defvar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到 Practical Common Lisp 使用(defvar *db* nil)来设置 global变量.出于同一目的使用setq可以吗?

I see that the Practical Common Lisp uses (defvar *db* nil) for setting up a global variable. Isn't it OK to use setq for the same purpose?

使用defvarsetq相比有什么优缺点?

What are the advantages/disadvantages of using defvar vs. setq?

推荐答案

有几种引入变量的方法.

DEFVAR SETQ 不会引入变量.

DEFVAR and DEFPARAMETER introduce global dynamic variables. DEFVAR optionally sets it to some value, unless it is already defined. DEFPARAMETER sets it always to the provided value. SETQ does not introduce a variable.

(defparameter *number-of-processes* 10)

(defvar *world* (make-world))     ; the world is made only once.

请注意,您可能永远不想使用xystreamlimit等名称的DEFVAR变量,为什么?因为这些变量随后将被声明为特殊变量,因此很难撤消.特殊声明是全局的,并且对该变量的所有进一步使用都将使用动态绑定.

Notice that you likely never want to DEFVAR variables with names like x, y, stream, limit, ... Why? Because these variables then would be declared special and its difficult to undo that. The special declaration is global and all further uses of the variable would use dynamic binding.

坏:

(defvar x 10)     ; global special variable X, naming convention violated
(defvar y 20)     ; global special variable Y, naming convention violated

(defun foo ()
  (+ x y))        ; refers to special variables X and y

(defun bar (x y)  ; OOPS!! X and Y are special variables
                  ; even though they are parameters of a function!
  (+ (foo) x y))

(bar 5 7)         ; ->   24

更好:始终在名称中用*标记特殊变量!

BETTER: Always mark special variables with * in their names!

(defvar *x* 10)     ; global special variable *X*
(defvar *y* 20)     ; global special variable *Y*

(defun foo ()
  (+ *x* *y*))      ; refers to special variables X and y

(defun bar (x y)    ; Yep! X and Y are lexical variables
  (+ (foo) x y))

(bar 5 7)           ;  ->   42

局部变量由 DEFUN 引入, LAMBDA

Local variables are introduced with DEFUN, LAMBDA, LET, MULTIPLE-VALUE-BIND and many others.

(defun foo (i-am-a-local-variable)
   (print i-am-a-local-variable))

(let ((i-am-also-a-local-variable 'hehe))
  (print i-am-also-a-local-variable))

现在,默认情况下,除非声明为 SPECIAL ,否则以上两种形式的局部变量均为词法.那么它们将是动态变量.

Now, by default the local variables in above two forms are lexical, unless they are declared SPECIAL. Then they would be dynamic variables.

下一步,还有几种将变量设置为新值的形式. SET SETQ SETF 等. SETQSETF可以设置词汇变量和特殊(动态)变量.

Next, there are also several forms to set a variable to new values. SET, SETQ, SETF and others. SETQ and SETF can set both lexical and special (dynamic) variables.

对于可移植代码,必须设置一个已声明的变量.标准未定义设置未声明变量的确切效果.

It is required for portable code that one sets variables that are already declared. The exact effect of setting a not declared variable is undefined by the standard.

因此,如果您知道Common Lisp实现的作用,则可以使用

So, if you know what your Common Lisp implementation does, you can use

(setq world (make-new-world))

顶层 Read-Eval-Print-Loop 中的

.但不要在代码中使用它,因为效果是不可移植的.通常,SETQ将设置变量.但是某些实现可能还会在不知道变量的情况下声明它 SPECIAL (默认情况下,CMU Common Lisp会执行此操作).那几乎总是不是人们想要的.如果您知道自己的工作,可将其用于临时用途,而不用于代码.

in the Read-Eval-Print-Loop at the toplevel. But don't use it in your code, since the effect is not portable. Typically SETQ will set the variable. But some implementation might also declare the variable SPECIAL when it doesn't know it (CMU Common Lisp does that by default). That's almost always not what one would want. Use it for casual use if you know what you do, but not for code.

与此处相同:

(defun make-shiny-new-world ()
  (setq world (make-world 'shiny)))

首先,应将此类变量写为*world*(带有周围的*字符),以明确表示它是全局特殊变量.其次,它应该之前用DEFVARDEFPARAMETER声明.

First, such variables should be written as *world* (with the surrounding * characters), to make clear that it is a global special variable. Second, it should have been declared with DEFVAR or DEFPARAMETER before.

典型的Lisp编译器会抱怨未声明上述变量.由于Common Lisp中不存在全局词法变量,因此编译器必须生成用于动态查找的代码.然后,一些编译器会说,好吧,我们假设这是一个动态查找,让我们将其声明为 special -因为无论如何我们都假定这样做.

A typical Lisp compiler will complain that above variable is undeclared. Since global lexical variables don't exist in Common Lisp, the compiler has to generate code for a dynamic lookup. Some compiler then say, okay, we assume that this is a dynamic lookup, let's declare it to be special - since that is what we assume anyway.

这篇关于Lisp中的setq和defvar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆