2019独角兽企业重金招聘Python工程师标准>>>

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.

转载于:https://my.oschina.net/redhouse/blog/132815

Creating and Interning Symbols相关推荐

  1. 系统怎么设计usb启动_在启动中启动设计系统

    系统怎么设计usb启动 重点 (Top highlight) Design systems are all the rage now and you've probably seen this ter ...

  2. ArcGIS Engine中的Symbols详解

    转自原文 ArcGIS Engine中的Symbols详解 本文由本人翻译ESRI官方帮助文档.尊重劳动成果,转载请注明来源. Symbols ArcObjects用了三种类型的Symbol(符号样式 ...

  3. Working with Symbols (在Balsamiq Mockups中复用自定义控件和页面模板)

    这篇文章是Balsamiq Mockups网站<Working with Symbols>的译文. 原文在这里:Working with Symbols Symbol是Balsamiq M ...

  4. Advanced Auto Layout:Programmatically Creating Constraints

    Programmatically Creating Constraints以编程方式创建约束 Whenever possible, use Interface Builder to set your ...

  5. 使用JPA进行update操作时,报org.springframework.beans.factory.BeanCreationException: Error creating bean with

    使用JPA进行update操作时,报org.springframework.beans.factory.BeanCreationException: Error creating bean with ...

  6. 用Unity和Playmaker创建一个限时游戏 Creating a Time Limit game with Unity and Playmaker

    本课程结束时,您将拥有在Unity中使用Playmaker创建游戏的工具 你会学到: playmaker状态的基础以及它们如何与动作一起工作. 安装悬停车,可以在竞技场内行驶. 不同力度的射击地雷驱动 ...

  7. Blender模块化建筑环境地形场景制作视频教程 Creating modular environments

    Blender模块化建筑环境地形场景制作视频教程 Creating modular environments Blender模块化建筑环境地形场景制作视频教程 Creating modular env ...

  8. /usr/local/lib/libz.a: could not read symbols: Bad value(64 位 Linux)

    /usr/bin/ld: /usr/local/lib/libz.a(crc32.o): relocation R_X86_64_32 against `a local symbol' can not ...

  9. Error Creating Control when creating a custom control

    如果你在创建ASP.NET的Server Control 是遇到报错: "Error Creating Control" when creating a custom contro ...

最新文章

  1. esp8266接收到的数据如何存放到数组中_java零基础——数组
  2. 9 个基于JavaScript 和 CSS 的 Web 图表框架
  3. STL源码剖析 Stack栈 queue队列
  4. jQuery的操作css的几种方法和位置,尺寸以及scrolltop方法
  5. 字节跳动入局全网搜索;思科回应中国区裁员;IntelliJ IDEA 新版发布! | 极客头条...
  6. C#-Activex插件操作指南
  7. Rust: 属性(attribute)的含义及文档大全
  8. 18位华人当选2022年加拿大工程院院士!京东副总裁梅涛入选!
  9. 使用Simian检查Java项目中冗余代码
  10. python抖音培训真的假的
  11. Report Builder简单的使用操作
  12. Linux第7章Gdk及Cairo基础,Linux第7章Gdk及Cairo基础概要1.ppt
  13. 浏览器拉起APP(深度链接)——scheme方法
  14. pd对焦速度_自动对焦速度是由相机还是镜头决定的?
  15. 微信闪退的修复方法分享
  16. MySQL数据库如何改名
  17. 计算机科学投稿须知,《计算机科学》投稿须知
  18. quartus18.1和modelsim的下载安装
  19. C# Microsoft.Office.Interop.Word设置Word页脚之添加当前页数
  20. SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本

热门文章

  1. 当机器人具有自我知觉,并能自适应环境,真的不可怕吗?
  2. verilog奇偶分频
  3. 从北京77元房租,说说关于房子的事
  4. 在linux中常用的shell备份脚本(波大帅哥)
  5. ViewFlipper的简单使用
  6. Android 模拟器 GPU ON
  7. sql 日周月统计和
  8. 使用PostgreSQL进行中文全文检索
  9. 易天ETU-link 100G QSFP28光模块系列资料
  10. 《记》rxjs分流操作符简单实现