ELisp: Place Expression, Generalized Variable

By Xah Lee. Date: .
setf
(setf PLACE VAL PLACE VAL etc)

Set each PLACE to VAL.

Each PLACE should be a symbol, or lisp expression for a sequence's element, such as

  • (car xlist)
  • (aref xvector i)
  • (nth n xlist)
  • (elt xsequence n)

these “place forms” are called generalized variable or place expressions.

return the last VAL in the list.

;; example of place expression

(setq xlist (list 3 4 5))

;; the nth form is a place expression, aka generalized variable
(setf (nth 0 xlist) 9)

(eq (nth 0 xlist) 9)
;; t

What is Place Expression, Generalized Variable, in Lisp

In many programing languages, you can change a value in a array by the normal assignment operator of equal sign=.

For example in python:

xlist = [3, 4, 5]

# here, the xlist[0] is considered a generalized variable, or place expression
# it's special because it is act not as a normal variable name but act as a position in a list
xlist[0] = 9

print(xlist[0] == 9)
# True

in lisp syntax, this would be like this:

;; example of place expression

(setq xlist (list 3 4 5))

;; the nth form is a place expression, aka generalized variable
(setf (nth 0 xlist) 9)

(eq (nth 0 xlist) 9)
;; t

In the lisp syntax, the oddness became apparent. Because lisp syntax usually follows a consistant form, and arguments to a function are evaluated.

But here, we see that the argument (nth 0 xlist) is not evaluated, but worse, it has a special meaning, when it is placed inside setf. It now has a context dependent semantics.

Emacs Lisp Variable