I would have thought this would work:
(defun list-of-things (thing1 thing2 thing3)
"returns a ???"
'(thing1 thing2 thing3))
But this is actually what is needed to return a list:
(defun list-of-things (thing1 thing2 thing3)
"returns a list"
(list thing1 thing2 thing3))
Those are two different things, and a central concept in Lisp languages.
The syntax '(a b c)
is a shorthand for (quote (a b c))
and quote
is a special form that returns its argument unevaluated. In that case, it would be list, containing three symbols.
On the other hand, list
is a normal function, it evaluates its arguments and return the list of their values.
To go a bit more in-depth: before evaluating any expression, the code has to be read. The part that is very specific to Lisp (and, in fact, even more so with Common Lisp) is that the "parser" is actually just a Lisp function, returning Lisp objects. Those objects are then given to the "evaluator", which evaluates them:
'(thing1 thing2 thing3)
, the reader knows that '
is a reader-macro, and so it reads (quote (thing1 thing2 thing3))
. This is a list of two elements, a symbol and another list of three symbols. This list is then given to the evaluator: it knows that quote
is a special form that returns its argument unevaluated, and so it simply returns the list (thing1 thing2 thing3)
, given to it by the reader.(list thing1 thing2 thing3)
, the reader also reads this as a list (this time, containing 4 symbols) that it then gives to the evaluator. Now, the evaluator sees that the first symbol is list
, a function, and so it evaluates the arguments (i.e. it determines what the symbols thing1
... are bound to), passes them to the list
functions, etc.All this is possible because Lisp code is defined in terms of Lisp objects (such as lists, etc). After the "parsing" (actually called reading) phase, the evaluator is really given an actual Lisp object to evaluate. Things are of course slightly more complicated when talking about compilation, etc, but the general idea is the same. The quote
operator, abbreviated '
, is a way to "directly" access the thing created by the reader, and to "bypass" evaluation.