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. JCIFS读取远程服务器文件过慢的解决方法

    JCIFS读取远程服务器文件过慢的解决方法 发表于3年前(2013-07-12 11:23)   阅读(1174) | 评论(0) // 我要收藏"; var favor_del = &qu ...

  2. Windows XP搜索功能 "包含文字" 搜索不到内容的解决办法

    Windows开始菜单 -- 运行 -- regedit -- 确定,编辑注册表 HKEY_LOCAL_MACHINE\SYSTEM\ControlSet\Control\ContentIndex 右 ...

  3. ASP.NET Web API上实现 Web Socket

    1. 什么是Web Socket Web Socket是Html5中引入的通信机制,它为浏览器与后台服务器之间提供了基于TCP的全双工的通信通道.用以替代以往的LongPooling等comet st ...

  4. LinuxShell算术运算

    Bash shell 的算术运算有四种方式:1:使用 expr 外部程式 加法 r=`expr 4 + 5`echo $r注意! '4' '+' '5' 这三者之间要有空白r=`expr 4 * 5` ...

  5. UVa 11584 Partitioning by Palindromes

    题意: 给出一个字符串,求最少能划分成多少个回文子串. 分析: d[i] = min{d[j] + 1 | s[j+1]...s[i]是回文串} d[i]表示前 i 个字符最少能分割的回文子串的个数 ...

  6. NALU(NAL单元)

    一 NALU类型    标识NAL单元中的RBSP数据类型,其中,nal_unit_type为1, 2, 3, 4, 5及12的NAL单元称为VCL的NAL单元,其他类型的NAL单元为非VCL的NAL ...

  7. 【转】linux中wait与waitpid的差别

    原文网址:http://blog.163.com/libo_5/blog/static/15696852010324287748/ zombie不占用内存也不占用CPU,表面上我们可以不用在乎它们的存 ...

  8. db file scattered read 等待事件

    db file scattered read 等待事件: 我们经常会见到db file scattered read  等待事件,在生产环境中,这个等待事件可能更为常见.这个事件表明用户进程正在读数据 ...

  9. [SharePoint 2010]关于基于声明(Claims)的用户认证模式

    转:http://blog.csdn.net/zw_2011/article/details/7417132 SharePoint 2010在用户认证模式上,较之以前的版本有了非常大的改变.在Shar ...

  10. Ecshop ajax 局部刷新购物车功能

    1.比如我们category.dwt 里有 <a href='flow.php'><SPAN id='cart_count_all'>{insert name='cart_in ...