ELisp: Quoting Symbol

By Xah Lee. Date: . Last updated: .

A Symbol , typically gets evaluated to its value. But you can stop this, by quote.

quote
(quote arg)

shortcut syntax: 'arg is same as (quote arg)
return the argument without evaluating it.

You can think of quote as “hold evaluation”.

When to Quote Symbol?

Some functions, automatically quote the argument for you, as a convenience.

For example, setq always automatically quotes its first argument. Because you basically always want the argument passed as a symbol. So, you write (setq x 3) instead of (set 'x 3).

The function set is almost the same as setq, except that set does not automatically quote the first argument. So, you write (set 'x 3).

Some functions, require you to quote the argument yourself. Because sometimes you want a symbol's value passed, and sometimes the symbol itself.

For example, mapcar's arguments are not automatically quoted. You may or may not want to quote them, depending on your use.

Here's two examples of using mapcar, where in one example we want to quote the argument, and in the other example we do not want to quote the argument.

;; suppose we don't know which of the following we want until run time
;; the function is chosen by user in a menu
(setq f 'cos)
(setq f 'sqrt)

;; normally, when using mapcar, we want first arg quoted
(mapcar 'cos '(1 2 3))

;; here, we don't want first arg quoted
(mapcar f '(1 2 3))

Reference

Lisp Symbol