Emacs Lisp Symbol (tutorial)

By Xah Lee. Date: . Last updated: .

Lisp has symbols concept. Lisp's concept of “symbols” is somewhat like identifiers in other languages, except that:

The Concept of Symbols in Lisp

LISP differs from most programing languages such as C, Java, Perl, Python, in that it deals with symbols, as opposed to just variables and values.

For example, in most languages, once you defined x=3, you cannot manipulate the variable “x” because it gets evaluated to 3 right away. If you want, you have to build a string "x" and manipulate this string, then finally use something like eval(string) to achieve the effect.

In lisp, after (setq x 3) then x would evaluate to 3, but (quote x) evaluates to the symbol x itself.

In practice, having a language dealing with “symbols” directly means that transformation of expressions is possible. (In this, this is lisp macros, which is a limited form of term rewriting languages.) (info "(elisp) Macros")

Symbol's Cells

Each lisp symbol has the following “cells” to store things:

A symbol's {value, function} cell may be empty, 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.)

In normal coding, you don't need to worry about any of these. The only thing that's useful for most elisp code is property list, but many other higher-level functions do it for you. (i.e. add, remove, properties.) (info "(elisp) Property Lists")

Understanding Lisp Symbol cells is important when you do advanced lisp programing. ➢ for example: macros, create and calling functions at run-time, function inside 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 program normal and simple.

Here's ways to get cell values.

;; check if a variable eval to a lisp symbol
(setq x1 123 )
(symbolp x1)                            ;nil

(setq x2 'xx )
(symbolp x2)                            ;t
;; get symbol cell values
(symbol-name 'x1)                       ; "x1"
(symbol-value 'x1)                      ; 123
(symbol-function 'x1)                   ; error. because it's void.
(symbol-plist 'x1)                      ;nil
;; set symbol cell values
(symbol-name 'setq)                  ; "setq"
(symbol-value 'setq)                 ; error. because it's void.
(symbol-function 'setq)              ;#<subr setq>
(symbol-plist 'setq)                 ;(byte-compile byte-compile-setq)

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.

How to convert a symbol to string?

;; convert a symbol to string
(symbol-name 'defun)

How to convert a string to symbol?

;; convert a string to symbol
(intern "something")

Emacs Lisp Symbol Property List

Emacs Lisp Symbol Property List

Classification of Emacs Lisp Function

Emacs Lisp Function

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from my keyboard store.