Re: Difference between ' and : symbols



jonathon wrote:
I've noticed that with ASDF there are 2 ways of referring to a system.
Some use the single quote for the name, others use the colon before the
symbol.  Both seem to work.

What is the difference?  Isn't a symbol evaluated to itself, while a
single-quoted value is evaluated to that value?

When writing Lisp code, you are writing representations of data structures. 'symbol and :symbol represent two very different data structures.


'symbol is read as the list (common-lisp:quote symbol), a list containing two elements, the symbol "QUOTE" in the package COMMON-LISP, and the symbol named "SYMBOL" in the current package.

:symbol is read as a symbol named "SYMBOL" in the package named "KEYWORD".

You may also encounter #:symbol. #:symbol is read as a symbol named "SYMBOL" in no package. This is useful when you want to read a symbol that is unequal to any other symbol (a la gensym).

Both 'symbol and :symbol will return symbols named "SYMBOL" when evaluated. Now ASDF:OPERATE is a function that takes a component-designator as its second argument -- either a component, the component's name as a string, or a symbol whose name is the component's name. So all of the following load the system named "asdf-install":

(asdf:operate 'asdf:load-op :asdf-install)
(asdf:operate 'asdf:load-op #:asdf-install)
(asdf:operate 'asdf:load-op 'asdf-install)
(asdf:operate 'asdf:load-op "asdf-install")
(asdf:operate 'asdf:load-op (asdf:find-system :asdf-install))

When not evaluated, like as the argument to a macro, 'symbol and :symbol and #:symbol act very differently, as one is a list and the others are symbols:

CL-USER> (defmacro print-unevaluated-type-of (obj)
           (print (type-of obj)))
PRINT-UNEVALUATED-TYPE-OF

CL-USER> (print-unevaluated-type-of 'symbol)
CONS

CL-USER> (print-unevaluated-type-of :symbol)
KEYWORD

CL-USER> (print-unevaluated-type-of #:symbol)
SYMBOL

  -- MJF
.