Elisp: Symbol
What is Lisp Symbol
All lisp's variable names and function names are symbols.
- Lisp symbol is a sequence of characters in source code, used for variable or function names, known as identifier in other languages.
- Symbols can be held unevaluated.
- Each symbol can store multiple values, including a Symbol Property List.
(let (x y) (set (quote x) 3) (set (quote y) 4) (+ x y)) ;; 7 ;; in the above code ;; x y ;; are user created symbols. ;; and ;; let set quote + ;; are builtin symbols
(set (quote x) 3) ;; sets value 3 to symbol x ;; the x is a symbol, held from evaluation by the quote function ;; because variable assignment is used often, a shorter syntax is introduced, the setq, which auto quote the first argument (setq x 3) ;; setq, set, quote, themselves are symbols
A Symbol Can be Both Function Name and Variable Name, and or Font-Face Name
- Some symbols are both function and variable. E.g.
buffer-file-name. - Some symbols are variable, not function. E.g. sentence-end-double-space.
- Some symbols are Font Face names, not function nor variable. e.g.
bold. But it can also be both a font-face name and a variable name. e.g. font-lock-function-name-face
(symbolp 'buffer-file-name) ;; t ;; is a function (fboundp 'buffer-file-name) ;; t ;; is a variable (boundp 'buffer-file-name) ;; t ;; s------------------------------ ;; is a function (fboundp 'sentence-end-double-space) ;; nil ;; is a variable (boundp 'sentence-end-double-space) ;; t ;; s------------------------------ ;; is a function (fboundp 'bold) ;; nil ;; is a variable (boundp 'bold) ;; nil ;; is a face (facep 'bold) ;; [face unspecified unspecified unspecified unspecified unspecified unspecified unspecified unspecified unspecified unspecified unspecified ...] ;; s------------------------------ ;; is a function (fboundp 'font-lock-function-name-face) ;; nil ;; is a variable (boundp 'font-lock-function-name-face) ;; t ;; is a face (facep 'font-lock-function-name-face) ;; [face unspecified unspecified unspecified unspecified unspecified unspecified unspecified unspecified unspecified unspecified unspecified ...]
Usefulness of Symbols in Lisp
One major way lisp differs from most programing languages (such as Python, JavaScript, Golang ) is that the language is said to be symbolic. Meaning, the identifiers (function names, variable names) , are “symbols”. This means, variable or function name isn't just values, can be just a inert sequence of characters.
For example, lets say you have a variable and you want to create new variable at run time, with name that's old var name joined by its value.
(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 (quote x)) (number-to-string (symbol-value (quote x))))) (1+ (symbol-value (quote x)))) (symbol-value (quote x4)) ;; 5
Transformation of Syntax, Lisp Macros
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 .)
Convert Symbol to String
;; convert a symbol to string (symbol-name (quote defun))
🛑 WARNING: Never change the string returned by symbol-name. Doing that may crash Emacs. (From lisp manual.)
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")
Check If a Value is Symbol
symbolp-
Return
tif argument is a symbol.(setq xx 3) (symbolp xx) ;; nil (symbolp (quote xx)) ;; t