What does the syntax, colons preceding variable in Common Lisp, mean? I've seen programs with such, and I'll present some sample code here, out of a large set of functions.
(defun expand (successorf node)
(mapcar (lambda (action-state-cost)
(let ((action (car action-state-cost))
(state (cadr action-state-cost))
(cost (caddr action-state-cost)))
(make-node :state state :parent node
:action action :path-cost (+ (node-path-cost node) cost)
:depth (1+ (node-depth node)))
))
(funcall successorf (node-state node))
))
Keyword Symbols
:foo
is a keyword symbol.
KEYWORD
packageUsage
Keyword symbols are used when one needs the combination of the following properties:
KEYWORD
KEYWORD
:foo
better than ':foo
:foo
evaluates to :foo
itself and only to :foo
In Common Lisp generally symbols can be in a package (kind of a namespace).
An unexported symbol bar
in a package foo
is written as foo::bar
. The double colon is between the package name and the symbol name.
An exported symbol then is written as foo:bar
. A single colon is used.
If the symbol is available in the current package then is written as bar
without the package.
The package KEYWORD
There is a special package called KEYWORD
. A symbol bar
in that package is simply and always written as :bar
.
Examples
These keyword symbols have also these interesting properties: the symbols are automatically exported from the package KEYWORD
(so keyword::bar
, keyword:bar
, ::bar
and :bar
are all the same symbol) and they evaluate to themselves:
CL-USER 5 > :bar
:BAR
CL-USER 6 > (describe :bar)
:BAR is a SYMBOL
NAME "BAR"
VALUE :BAR
FUNCTION #<unbound function>
PLIST NIL
PACKAGE #<The KEYWORD package, 0/4 internal, 5830/8192 external>
CL-USER 7 > (eq 'keyword::bar ':bar)
T
CL-USER 8 > (eq :bar ':bar) ; quoted or unquoted, each subform evaluates to :bar
T
Usage
Keyword symbols are used for example as names in named arguments:
(defun foo (&key bar) (+ bar 10))
(foo :bar 7)
Typically they are also used in arguments to instance and structure construction.
(defstruct node state parent action)
DEFSTRUCT
is a Common Lisp macro and it generates several functions. One of them is a function MAKE-NODE
, which can be used as:
(make-node :state 'open
:parent some-parent
:action an-action)
Note: sometimes the data might also be a keyword. For example in above form, the state might be :open
and not open
:
(make-node :state :open
:parent some-parent
:action an-action)