0

'(1 2 3) gives me a list of three elements,

but

(setq A "apple")

'(A) gives me (A).

My question is how can I get the list '("apple") by referring variable A?

  • Tried (list A)? – Tom Regner Mar 28 '15 at 10:35
  • It works! Thank you very much. How come there are several ways to construct a list? – yi.tang.uni Mar 28 '15 at 10:39
  • 1
    Using ´, the list becomes exactly what you write. Using list, each argument is evaluated before added to the list. Of course, then there is backquotes that work almost like ', but you can use , and ,@ to evaluate parts of the expression. – Lindydancer Mar 28 '15 at 11:30
  • 1
    Related/duplicate: emacs.stackexchange.com/q/7481/115 – Kaushal Modi Mar 28 '15 at 13:47
6

How can I get the list '("apple") by referring variable A?

(list A)

or

`(,A)

How come there are several ways to construct a list?

  1. The ' in '(A) does not construct a list. The Lisp reader constructs a list when it reads (A). The ' prevents the Lisp REPL (i.e., the top-level loop) from evaluating the list that was read, so the list is just returned.

  2. Function list also does not construct a list, directly (depending on the Lisp implementation). The two list constructors in Lisp are cons and nil (aka ()). Function list uses those two constructors: (list) returns (), and (list X) returns (cons value-of-X nil), which is also written (value-of-X).


The backquote syntax of `(,A) is syntactic sugar for calls to functions such as list and cons. For example, in this case it could be an abbreviation for (list A). The comma (,) means evaluate what follows, and the backquote (`) means do not evaluate what follows, except for nested comma constructs. So `(,A) could be read as "evaluate A and wrap it in parens to make a singleton list". Backquote syntax shows you a picture of what it produces. You just need to plug in (substitute) the value of whatever follows a comma.

Not the answer you're looking for? Browse other questions tagged or ask your own question.