Alex Balgavy

Just some stuff about me.

Here's my dotfiles repository.

Check out my blog.

My keys: PGP, SSH

My crypto wallets (BTC, XMR)


What links here:

Elisp symbols/functions/values

In Elisp, data is code, and code is data. You convert code to data with quote, and data to code with eval:

(setq mydata '(+ 1 2))
(symbol-value 'mydata) ;; what's stored in "mydata"? => (+ 1 2)
(eval mydata) ;; evaluating what's inside mydata => 3

;; to show that this is indeed the same:
(eq (eval '(+ 1 2))
    (+ 1 2))

A symbol has different slots: for the name, value, function, and plist.

;; a dummy function
(defun funcfunc (&rest args) "function function")
(defun valuefunc (&rest args) "value function")

(set 'foo 5) ;; set the value cell
(fset 'foo 'funcfunc) ;; set the function cell
(put 'foo 'dummy t) ;; add to plist

(symbol-name 'foo) ;; => "foo"
(symbol-function 'foo) ;; => funcfunc
(symbol-value 'foo) ;; => 5
(symbol-plist 'foo) ;; => ("example" "value" dummy t)

;; getting the value
(eval foo) ;; gets the value cell
(+ foo 2) ;; evaluates 'foo, so uses the value cell

;; getting the plist
(get 'foo 'dummy) ;; => t

;; calling the function
(foo) ;; gets the function cell & calls it
(funcall (symbol-function 'foo)) ;; also calls it
(apply 'foo nil) ;; also calls it
(funcall 'foo) ;; also calls it

;; the value can also be made a function
(set 'foo 'valuefunc)
(symbol-value 'foo) ;; => valuefunc
(foo) ;; only looks at function cell. => functionfunction
(funcall 'foo) ;; does not evalute foo. => functionfunction
(funcall foo) ;; evaluates foo. => valuefunction