What is the difference between a keyword symbol and a quoted symbol?
common-lisp, lisp, symbols
Solution
First: `'something` is a shorter notation for `(quote something)`. The reader will transform the quote character into a list with the symbol `cl:quote` as the first item. For the evaluator it means: don't evaluate `something`, just return it as a result.
CL-USER 22 > '(quote foo)
(QUOTE FOO)
CL-USER 23 > ''foo
(QUOTE FOO)
CL-USER 24 > (read-from-string "'foo")
(QUOTE FOO)
The colon `:` is a package marker. If the package name is missing, the symbol is in the `KEYWORD` package.
We can give `foo` a value:
CL-USER 11 > (setq foo 10)
10
`foo` evaluates to its value.
CL-USER 12 > foo
10
A quoted symbol evaluates to the symbol.
CL-USER 13 > 'foo
FOO
We can't give `:foo` a value:
CL-USER 14 > (setq :foo 10)
Error: Cannot setq :FOO -- it is a keyword.
1 (abort) Return to level 0.
2 Return to top loop level 0.
Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.
CL-USER 15 : 1 > :top
`:foo` already has a value: itself.
CL-USER 16 > :foo
:FOO
Naturally a quoted `:foo` evaluates to `:foo`.
CL-USER 17 > ':foo
:FOO
The symbol `foo` is in some package, here `CL-USER`.
CL-USER 18 > (symbol-package 'foo)
#<The COMMON-LISP-USER package, 92/256 internal, 0/4 external>
The symbol `:foo` is in the `KEYWORD` package.
CL-USER 19 > (symbol-package ':foo)
#<The KEYWORD package, 0/4 internal, 6230/8192 external>
Since `:foo` is the value of `:foo` we can also write:
CL-USER 20 > (symbol-package :foo)
#<The KEYWORD package, 0/4 internal, 6230/8192 external>
`:foo` is an abbreviation for `keyword:foo`. Thus the symbol is in the keyword package and it is exported.
CL-USER 21 > keyword:foo
:FOO
So keyword symbols are self-evaluation constant symbols in the keyword package. They are used as markers in data structures and in keyword arglists. The good things: you don't need to struggle with packages and they evaluate to themselves - so a quote is not needed.
Problem
What is the difference between the keyword symbol ``` :foo ``` and the quoted symbol: ``` 'foo ``` Both stand for themselves, and can be used as an identifier. I can see that keyword symbols are mainly used for named parameters, but I was asking myself if it was not possible to implement this using quoted symbols as well? In other words: Why do I need both?