文档章节

Creating and Interning Symbols

TinkerS
 TinkerS
发布于 2013/05/23 09:43
字数 1131
阅读 20
收藏 0
To understand how symbols are created in GNU Emacs Lisp, you must know how Lisp reads them. Lisp must ensure that it finds the same symbol every time it reads the same set of characters. Failure to do so would cause complete confusion. 


 When the Lisp reader encounters a symbol, it reads all the characters of the name. Then it “hashes” those characters to find an index in a table called an obarray. Hashing is an efficient method of looking something up. For example, instead of searching a telephone book cover to cover when looking up Jan Jones, you start with the J's and go from there. That is a simple version of hashing. Each element of the obarray is a bucket which holds all the symbols with a given hash code; to look for a given name, it is sufficient to look through all the symbols in the bucket for that name's hash code. (The same idea is used for general Emacs hash tables, but they are a different data type; see Hash Tables.) 


 If a symbol with the desired name is found, the reader uses that symbol. If the obarray does not contain a symbol with that name, the reader makes a new symbol and adds it to the obarray. Finding or adding a symbol with a certain name is called interning it, and the symbol is then called an interned symbol. 


Interning ensures that each obarray has just one symbol with any particular name. Other like-named symbols may exist, but not in the same obarray. Thus, the reader gets the same symbols for the same names, as long as you keep reading with the same obarray. 


Interning usually happens automatically in the reader, but sometimes other programs need to do it. For example, after the M-x command obtains the command name as a string using the minibuffer, it then interns the string, to get the interned symbol with that name. 


 No obarray contains all symbols; in fact, some symbols are not in any obarray. They are called uninterned symbols. An uninterned symbol has the same four cells as other symbols; however, the only way to gain access to it is by finding it in some other object or as the value of a variable. 


Creating an uninterned symbol is useful in generating Lisp code, because an uninterned symbol used as a variable in the code you generate cannot clash with any variables used in other Lisp programs. 


In Emacs Lisp, an obarray is actually a vector. Each element of the vector is a bucket; its value is either an interned symbol whose name hashes to that bucket, or 0 if the bucket is empty. Each interned symbol has an internal link (invisible to the user) to the next symbol in the bucket. Because these links are invisible, there is no way to find all the symbols in an obarray except using mapatoms (below). The order of symbols in a bucket is not significant. 


In an empty obarray, every element is 0, so you can create an obarray with (make-vector length 0). This is the only valid way to create an obarray. Prime numbers as lengths tend to result in good hashing; lengths one less than a power of two are also good. 


Do not try to put symbols in an obarray yourself. This does not work—only intern can enter a symbol in an obarray properly. 


Common Lisp note: Unlike Common Lisp, Emacs Lisp does not provide for interning a single symbol in several obarrays. 


Most of the functions below take a name and sometimes an obarray as arguments. A wrong-type-argument error is signaled if the name is not a string, or if the obarray is not a vector. 
 — Function: symbol-name symbol


This function returns the string that is symbol's name. For example: 
          (symbol-name 'foo)
               ⇒ "foo"


Warning: Changing the string by substituting characters does change the name of the symbol, but fails to update the obarray, so don't do it! 
 — Function: make-symbol name


This function returns a newly-allocated, uninterned symbol whose name is name (which must be a string). Its value and function definition are void, and its property list is nil. In the example below, the value of sym is not eq to foo because it is a distinct uninterned symbol whose name is also ‘foo’. 
          (setq sym (make-symbol "foo"))
               ⇒ foo
          (eq sym 'foo)
               ⇒ nil
 — Function: intern name &optional obarray


This function returns the interned symbol whose name is name. If there is no such symbol in the obarray obarray, intern creates a new one, adds it to the obarray, and returns it. If obarray is omitted, the value of the global variable obarray is used. 
          (setq sym (intern "foo"))
               ⇒ foo
          (eq sym 'foo)
               ⇒ t
          
          (setq sym1 (intern "foo" other-obarray))
               ⇒ foo
          (eq sym1 'foo)
               ⇒ nil


Common Lisp note: In Common Lisp, you can intern an existing symbol in an obarray. In Emacs Lisp, you cannot do this, because the argument to intern must be a string, not a symbol. 
 — Function: intern-soft name &optional obarray


This function returns the symbol in obarray whose name is name, or nil if obarray has no symbol with that name. Therefore, you can use intern-soft to test whether a symbol with a given name is already interned. If obarray is omitted, the value of the global variable obarray is used. 


The argument name may also be a symbol; in that case, the function returns name if name is interned in the specified obarray, and otherwise nil. 
          (intern-soft "frazzle")        ; No such symbol exists.
               ⇒ nil
          (make-symbol "frazzle")        ; Create an uninterned one.
               ⇒ frazzle
          (intern-soft "frazzle")        ; That one cannot be found.
               ⇒ nil
          (setq sym (intern "frazzle"))  ; Create an interned one.
               ⇒ frazzle
          (intern-soft "frazzle")        ; That one can be found!
               ⇒ frazzle
          (eq sym 'frazzle)              ; And it is the same one.
               ⇒ t
 — Variable: obarray


This variable is the standard obarray for use by intern and read. 
 — Function: mapatoms function &optional obarray


This function calls function once with each symbol in the obarray obarray. Then it returns nil. If obarray is omitted, it defaults to the value of obarray, the standard obarray for ordinary symbols. 
          (setq count 0)
               ⇒ 0
          (defun count-syms (s)
            (setq count (1+ count)))
               ⇒ count-syms
          (mapatoms 'count-syms)
               ⇒ nil
          count
               ⇒ 1871


See documentation in Accessing Documentation, for another example using mapatoms. 
 — Function: unintern symbol obarray


This function deletes symbol from the obarray obarray. If symbol is not actually in the obarray, unintern does nothing. If obarray is nil, the current obarray is used. 


If you provide a string instead of a symbol as symbol, it stands for a symbol name. Then unintern deletes the symbol (if any) in the obarray which has that name. If there is no such symbol, unintern does nothing. 


If unintern does delete a symbol, it returns t. Otherwise it returns nil.

本文转载自:http://www.gnu.org/software/emacs/manual/html_node/elisp/Creating-Symbols.html#Creating-Symbols

共有 人打赏支持
TinkerS
粉丝 8
博文 55
码字总数 12670
作品 0
高级程序员
私信 提问
Editor Component for Physical Applications Data Input

Download source (ZIP) - 768.7 KB Download source (RAR) - 738.5 KB Introduction Modern engineering applications allow users to operate with physical values measured with differen......

Sergey L. Gladkiy
2017/12/14
0
0
[Debug].NET Debugging and C++ Debugging Resources

The following is a summary of tools and resources that you might be interested in after taking the .NET Debugging and/or C++ Debugging courses we offer at Sela. [Shameless plug:......

吞吞吐吐的
2017/01/11
0
0
脚本语言 newLISP 10.6.1 开发版发布了!

newLISP 10.6.1 开发版发布了,此版本现已提供下载,主要就是 Bug 修复,同时还添加几个小方法,兼容 newLISP 10.6.0 版本。 新功能和改进 A new function (collect exp) reapeatedly evalu...

wendal
2014/09/17
2.2K
6
Objective-C ---NSString(梳理整理)

NSString 创建方式: (对象方法(对应类方法)) 1. 针对不可变字符串: = @" "; 2.通过一个字符串创建 initWithString(stringWithString) 3 .通过格式化创建 initWithFormat(stringWithForma...

都英俊兮
2016/05/12
21
1
Java面试基础篇——第二篇:String类能为什么不能被继承?

String类有final修饰符,而final修饰的类是不允许被继承的,实现的细节也无法改变。 String类为何被设计为不能继承? 1.带有 final 修饰符的类是不可派生的。 如 java.lang.String, 为 Stri...

developlee的潇洒人生
2018/07/12
0
0

没有更多内容

加载失败,请刷新页面

加载更多

JDK 12又来了,我学不动了...

写在前面 看到 JDK 12又发布了,萌新不知不觉感觉瑟瑟发抖,从 Java 1.8的函数式编程思维和范式 到 Java 1.9的模块化特性的加持 以及还没来得及深切感受一下 Java 1.11 的 ZGC强大之后,这次...

CodeSheep
14分钟前
0
0
解决在gradle构建project时,发生peer not authenticated错误的方法

问题: FAILURE: Build failed with an exception.* What went wrong:Could not resolve all dependencies for configuration ':wl01-service:compile'.> Could not resolve com.ali......

Benz001
24分钟前
1
0
Apache Ignite上的TensorFlow

任何深度学习都是从数据开始的,这是关键点。没有数据,就无法训练模型,也无法评估模型质量,更无法做出预测,因此,数据源非常重要。在做研究、构建新的神经网络架构、以及做实验时,会习惯...

李玉珏
49分钟前
6
0
Java记录日志附带请求标识

起因 系统是web接口服务,排查故障的时候经常需要记录某次请求调用链路日志。这样我们拉日志的时候只要匹配这个traceid就行了 第一版解决方案 原来我们一直用了个很low的办法,在请求开始的时...

Tree
53分钟前
3
0
使用split_size优化的ODPS SQL的场景

使用split_size优化的ODPS SQL的场景 首先有两个大背景需要说明如下: 说明1:split_size,设定一个map的最大数据输入量,单位M,默认256M。用户可以通过控制这个变量,从而达到对map端输入的...

阿里云官方博客
58分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部