elisp语法

时间:2024-12-10 15:33:56

http://*.com/questions/3855862/setq-and-defvar-in-lisp

defvar, let, setq的语法分析:

defvar introduces a dynamic variable while setq is used to assign a value to a dynamic or lexical variable. The value of a dynamic variable is looked up in the environment that calls the function, while the value of a lexical variable is looked up in the environment where the function was defined. The following example will make the difference clear:

;; dynamic variable sample
> (defvar *x* )
*X*
> (defun fx () *x*)
FX
> (fx)> (let ((*x* )) (fx)) ;; gets the value of *x* from the dynamic scope. > (fx) ;; *x* now refers to the global binding. ;; example of using a lexical variable
> (let ((y ))
(let ((fy (lambda () (format t "~a~%" y))))
(funcall fy) ;; =>
(let ((y ))
(funcall fy) ;; => , the value of lexically bound y
(setq y ) ;; => y in the current environment is modified
(funcall fy)) ;; => , the value of lexically bound y, which was
;; unaffected by setq
(setq y ) ;; => alue of the original y is modified.
(funcall fy))) ;; => , the new value of y in fy's defining environment.

Dynamic variables are useful for passing around a default value. For instance, we can bind the dynamic variable *out* to the standard output, so that it becomes the default output of all io functions. To override this behavior, we just introduce a local binding:

> (defun my-print (s) 
    (format *out* "~a~%" s))
 MY-PRINT
 > (my-print "hello")
 hello
 > (let ((*out* some-stream)) 
    (my-print " cruel ")) ;; goes to some-stream
 > (my-print " world.")
 world

A common use of lexical variables is in defining closures, to emulate objects with state. In the first example, the variable y in the binding environment of fy effectively became the private state of that function.

defvar will assign a value to a variable only if it is not already assigned. So the following re-definition of *x* will not change the original binding:

> (defvar *x* 400)
 *X*
 > *x*
 100

We can assign a new value to *x* by using setq:

> (setq *x* 400)
 400
 > *x*
 400
 > (fx)
 400
 > (let ((*x* 500)) (fx)) ;; setq changed the binding of *x*, but its dynamic property still remains.
 500
 > (fx)
 
400

===========================================================================================

There are several similar setting functions:
    set & setq
    set-default
    defcustom
    custom-set-value
    custom-set-variables
    customize-set-value
    customize-set-variable

so, what's the difference between these functions?

The short answer to you question is:
    use setq or setq-default for variables defined by `defvar.
    use setq, setq-default, or the Customize mechanism for variables defined by defcustom
Below is the long answer:
The functions that you are going to use are the following:
    set is the main function to set the value of a variable.
    setq is another version that automatically quotes its first argument. This is useful since quoting the first argument is what you want to do almost all the time.
    Some variables cannot be set globally. Whenever you set the variable it is only set for the current buffer. If you want to simulate setting this variable globally you use set-default or setq-default.
The functions that a package writer uses are:
    defvar which allows the package writer to define a variable and to give some documentation. This function is not required but makes the life of users easier.
    defcustom builds on defvar. It tells emacs that it is a variable, and it allows the developer to create a custom interface to set the value. The developer can say, things like "this variable can contain only the value 'foo or 'bar".
Setting variables can be done two ways:
    if defvar was used, the values can only be set by the user in its .emacs by using the set function (or variants)
    if defcustom was used, the values can be set using set (see 1.) OR by using Customize. When using the customize mechanism, emacs will generate some code that it will place in custom-set-variables. The user should not use this function.

===========================================================================================