Emacs Lisp: Symbol
What is Lisp Symbol
The Concept of Symbols in Lisp
For example, in most languages, once you defined
x=3, you cannot manipulate the variable “x” because it becomes 3 internally.
In lisp, the x can be x itself.
In lisp, after
(setq x 3)
x would evaluate to 3, but
(quote x) evaluates to the symbol x itself.
For example, lets say you have a list of variable x = 4, y = 2, z = 3. you want to creat new variables at run time, such that the new variable name are these variable with their value appended to the name, with new value that's old value plus 1.
(setq x 4) ;; create a new symbol, whose name is old name with its value appended, set the value to plus 1 of original (set (intern (concat (symbol-name 'x) (number-to-string (symbol-value 'x)))) (1+ (symbol-value 'x))) (symbol-value 'x4) ;; 5
In practice, having a language dealing with “symbols” directly means that transformation of expressions in source code is possible at run-time. (In lisp, this is the lisp macro feature, which is a limited form of term rewriting languages such as Wolfram Language .) Macros (ELISP Manual)
Each lisp symbol has the following “cells” to store things:
- “print name” cell
- A string, the same as the symbol. Automatically set, cannot be changed.
- “value” cell
- Store the symbol's value. When value cell is not void, the symbol is considered as a variable.
- “function” cell
- Store function definition object, lisp macros, or other objects that can act as function.
- “property list” cell
- Hold a list of name/value pairs. Used for storing meta info about the symbol, such as function state, font face spec (for syntax coloring), deprecation flag, etc.
A symbol's value cell or function cell may be empty. If so, it's said to be “void”. When you try to get a cell's value that's void, it's a lisp error. (a empty cell is not the same as having value of nil.)
Normally, you don't need to worry about any of these tech details. The only thing that's useful for most elisp code is property list.
Understanding Lisp Symbol is important when you do advanced lisp programing. For example: macros, create and call functions at run-time, nested functions, manipulate evaluation, implementing a language, or any sort of meta-programing. If you don't have a need, you should not exploit these facilities in your program. Keep your code simple.
Symbols (ELISP Manual)
Check If a Value is Symbol
Return t if argument is a symbol.
(symbolp 3) ; nil (symbolp 'x) ; t
Get Value of Symbol's Cells
Here's how to get various cell's values.
Here's a example of getting cell values, with symbol “sin” (
sin is a builtin math function).
get symbol's name cell value
(symbol-name 'sin) ; "sin"
get symbol's value cell value
(symbol-value 'sin) ; void-variable error ;; because the value cell of the symbol sin is void ;; or, we just say that sin isn't a variable
get symbol's function cell value
(symbol-function 'sin) ;#<subr sin> ;; the value is a primitive function (written in C), and has print form of #<subr sin>
get symbol's property list cell value
(symbol-plist 'sin) ; (side-effect-free t)
Set Symbol's Name Cell
Symbol's name cell is automatically set, as a string of the symbol name. Symbol name cell cannot be changed.
Set Symbol's Value Cell
The normal way to set a symbol's value cell is using
;; set a symbol's value cell (setq y "yes yes") ;; get it (symbol-value 'y) ; "yes yes"
Setting Variables (ELISP Manual)
You can also check if a symbol's value cell is not empty, by
(we think of it as checking if a variable is defined.)
(boundp 'h) ; nil (setq h 4) (boundp 'h) ; t
Set Symbol's Function Cell
The normal way to set a symbol's function cell is using
;; a function that returns 4 (defun z () 4) ;; Note: return value of defun is not defined ;; get a symbol's function cell value (symbol-function 'z) ; (lambda nil 4)
Defining Functions (ELISP Manual)
Function Cells (ELISP Manual)
You can check if a symbol's function cell is not empty, by
(fboundp 'f) ; nil ;; define a function that return 3 (defun f () 3) ;; now the fuction cell is filled (fboundp 'f) ; t
Because a symbol can both hold a value and a function, a symbol can be both a variable and function. For example, the symbol
buffer-file-name is both a variable and function.
(you can try
describe-variable on it.)
Set Symbol's Property List Cell
Convert Symbol to String
;; convert a symbol to string (symbol-name 'defun)
Convert String to Symbol
;; convert a string to symbol ;; if the symbol does not already exist in obarray, create it, put it in obarray (intern "x") ;; if the symbol does not already exist in obarray, return nil (intern-soft "x")