http://stackoverflow.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.
 

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

elisp语法的更多相关文章

  1. 使用Emacs:生存篇

    使用Emacs:生存篇 vim和Emacs都是很强大的编辑器.所以,入门有一定难度.这里不谈vim,谈Emacs下的生存--第一次使用Emacs时的使用. 1.emacs的安装: 在Fedora下: ...

  2. C C++ 语法

    非常酷的网站: http://yige.org/cpp/defined_data_types.php 在Linux下有一个目录/proc/$(pid),这个目录保存了进程号为pid的进程运行时的所有信 ...

  3. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

  4. Swift与C#的基础语法比较

    背景: 这两天不小心看了一下Swift的基础语法,感觉既然看了,还是写一下笔记,留个痕迹~ 总体而言,感觉Swift是一种前后端多种语言混合的产物~~~ 做为一名.NET阵营人士,少少多多总喜欢通过对 ...

  5. 探索C#之6.0语法糖剖析

    阅读目录: 自动属性默认初始化 自动只读属性默认初始化 表达式为主体的函数 表达式为主体的属性(赋值) 静态类导入 Null条件运算符 字符串格式化 索引初始化 异常过滤器when catch和fin ...

  6. [C#] 回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性

    回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性 序 目前最新的版本是 C# 7.0,VS 的最新版本为 Visual Studio 2017 RC,两者都尚未进入正式阶段.C# 6.0 ...

  7. Velocity初探小结--velocity使用语法详解

    做java开发的朋友一般对JSP是比较熟悉的,大部分人第一次学习开发View层都是使用JSP来进行页面渲染的,我们都知道JSP是可以嵌入java代码的,在远古时代,java程序员甚至在一个jsp页面上 ...

  8. node.js学习(二)--Node.js控制台(REPL)&&Node.js的基础和语法

    1.1.2 Node.js控制台(REPL) Node.js也有自己的虚拟的运行环境:REPL. 我们可以使用它来执行任何的Node.js或者javascript代码.还可以引入模块和使用文件系统. ...

  9. C#语法糖大汇总

    首先需要声明的是"语法糖"这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕 ...

随机推荐

  1. 利用qt打开一张图片并转成灰度矩阵

    首先是mat类,这个类的主要作用是构造一个容器,并将对应像素的灰度值放在容器内 #ifndef MAT_H #define MAT_H #include <vector> #include ...

  2. Docker实例教程[超详细](一)

    Docker Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互 ...

  3. inline-block在ie6中的经典bug

    众所周知,给元素设置 inline-block ,可以让ie下的元素出发layout:1. 但是,当给元素设置 inline-block 后,在另外一个class 样式(非设置inline-block ...

  4. maven - setting.xml

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  5. vpn局域网共享

    vpn局域网共享 更改网关为vpn共享主机地址 在“命令运行符”处,挨个键入下列命令,并回车生效: regsvr32 Softpub.dll regsvr32 Wintrust.dll regsvr3 ...

  6. Oracle数据泵导入导出数据,建立表空

    Oracle11g 数据导入到oracle10g 中:1.在oracle11g 服务器命令行中用expdp 导出数据expdp ts/ts@orcl directory=expdp_dir dumpf ...

  7. java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataProvider

    Caused by: java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvi ...

  8. treap完全版模板

    这是我综合poj1442 3481 2352的treap操作 得到treap完全版模板.(经测AC) 结构体Tree { int key; //键值 int size; //该子树总节点个数 int ...

  9. bzoj4042

    比较好的树形dp,涉及到树上路径的题目,我们往往考虑对路径分类 当我们考虑以x为根的子树,有这样几类路径 1. 起点终点都在子树内 2. 一个点延伸到子树外 对于要选择另一个点在子树外的路径,要建立在 ...

  10. bzoj1875: [SDOI2009]HH去散步

    终于A了...早上按自己以前的写法一直WA.下午换了一种写法就A了qwq #include<cstdio> #include<cstring> #include<iost ...