A symbol is an atom , an atom is everything which is not a cons . A list is anything which is enclosed by parenthesis .
(1 2 3 ) (defun square(num ) (* num num ) ) ;;;Two Examples of cons () (1 2 3 ) (defun square(num ) (* num num ) ) ;;;Three Example of lists () 1 2 defun square num ;;;Six Atom examples
Table of Contents
What is a symbol
An atom can be a self evaluated atom , for example 1
, which is a number , and which is self evaluating . 1
does not have a name , or any other attributes associated with it , it evaluates to itself , which is 1
.
> 1 1 >(eval 1 ) ;;;Call the function eval , ;;;which evaluates its argument , ;;;passing 1 . 1
An atom can also be a symbol for example pi
. A symbol has a set of attributes which are associated with it , historically called its cells . They are its : name , value , function , property list , and the package to which it belongs .
The attributes for a symbol , can be gotten or set using :
symbol-name
: returns the value of the name attribute associated with a symbol , or throws an error , if the passed argument is not a symbol . The name attribute of a symbol is a string , which is the name of this symbol , and it cannot be changed .
> (symbol-name 'pi ) ;;;Get the value of the name ;;;attribute of the symbol ;;;pi . pi is quoted , ;;;as not to be evaluated . "PI" > (symbol-name 'odka ) ;;;Get the value of the name ;;;attribute of the symbol ;;;odka . "ODKA" > (symbol-name 'atom ) ;;;Get the value of the name ;;;attribute of the symbol ;;;atom . "ATOM" >> (symbol-name 1 ) ;;;1 is not a symbol , this ;;;will cause an error to be ;;;shown of : *** - SYMBOL-NAME: 1 is not a symbol The following restarts are available: ...
symbol-value
: returns the value , of the value attribute , associated with a symbol , if any . The value attribute can hold anything . If a symbol attribute value , has a value , the symbol is said to be bound , if the value attribute has no value , the symbol is said to be unbound . When a symbol is considered to be a variable , what is evaluated is its value attribute .
symbol-value
throws an error , if the attribute value of a symbol has no value , in other words if the symbol is not bound , or if the passed argument is not a symbol . A value for the symbol value attribute , can be set for example , using set
.
> (symbol-value 'pi ) ;;;Get the value , of the ;;;value attribute , of ;;;pi . 3.1415926535897932385L0 > (symbol-value 'atom ) ;;;Get the value , of the ;;;value attribute , of ;;;atom . atom is quoted ;;;as not to be evaluated . *** - SYMBOL-VALUE: variable ATOM has no value The following restarts are available: ... > (symbol-value 1) ;;;Get the value , of the ;;;value attribute , of ;;;the atom 1 , this will ;;;throw an error , since 1 ;;;is not a symbol . *** - SYMBOL-VALUE: 1 is not a symbol The following restarts are available: ... > (symbol-value 'odka ) ;;;Get the value , of the value ;;;attribute , of the symbol ;;;odka . odka is not bound ;;;this will throw an error . *** - SYMBOL-VALUE: variable ODKA has no value The following restarts are available: ... >(set 'odka 1 ) ;;;Set the value , of the ;;;value attribute , of the ;;;symbol odka to 1 . 1 > (symbol-value 'odka ) ;;;Get the value , of the ;;;value attribute , of ;;;the symbol odka . 1 >(+ odka 2 ) ;;;odka is treated as a variable , ;;;it is not quoted , hence its ;;;value attribute is evaluated , ;;;it is 1 . ;;;1 + 2 is equal to 3 . 3
symbol-package
: returns the value of the package attribute associated with a symbol . This is the package that a symbol belongs to . symbol-package
throws an error , if the passed argument is not a symbol , and returns nil
, if the symbol does not belong to any package .
> (symbol-package 'pi ) ;;;Get the package that the symbol ;;;pi belongs to . #<PACKAGE COMMON-LISP> > (symbol-package 1 ) ;;;Get the value , of the attribute ;;;package , of the atom 1 . ;;;This will throw an error , ;;;since 1 is not a symbol . *** - SYMBOL-PACKAGE: 1 is not a symbol The following restarts are available: .. > (symbol-package 'atom ) ;;;Get the value of the attribute ;;;package of the symbol atom . #<PACKAGE COMMON-LISP> > (symbol-package 'yada ) ;;;Get the value , of the attribute : ;;;package , of the symbol yada . ;;;The default package when lisp ;;;starts is common-lisp-user , ;;;it has a nickname of cl-user . #<PACKAGE COMMON-LISP-USER>
symbol-function
: returns the value of the function attribute associated with a symbol , if any . The function attribute of a symbol , contains the definition of a callable object associated with a symbol , such as a function or a macro . When a symbol is called , what is evaluated is its function attribute .
If the attribute function of a symbol , is associated with a value , it is said to be fbound . symbol-function
throws an error if the symbol is not fbound , or if the passed argument is not a symbol .
The value , of the attribute function , of a symbol , can be set , for example when defining a new function using defun
, or for example by using defmacro
which defines a new macro .
> (symbol-function 'pi ) ;;;Get the value of the attribute : ;;;function , of the symbol pi , ;;;pi is not fbound , this will ;;;throw an error . *** - SYMBOL-FUNCTION: undefined function PI The following restarts are available: .. > (symbol-function 'atom ) ;;;Get the value of the attribute : ;;;function , of the symbol atom . #<SYSTEM-FUNCTION ATOM> > (defun square(num ) (* num num ) ) ;;;Define a new function , it has a ;;;symbol attribute : name , which ;;;value is square , and it is ;;;fbound . SQUARE > (symbol-function 'square ) ;;;Get the value stored in the ;;;attribute : function of the ;;;symbol square . #<FUNCTION SQUARE (NUM) (DECLARE (SYSTEM::IN-DEFUN SQUARE)) (BLOCK SQUARE (* NUM NUM))> > (square 3 ) ;;;The square symbol is evaluated as ;;;a function , hence the value of the ;;;attribute : function , is evaluated . ;;;This will square the number 3 . 9 > (set 'square 0 ) ;;;Set a value , for the attribute : ;;;value , of the symbol square . 0 > (+ square 1 ) ;;;square is treated as a ;;;variable , the value of ;;;its attribute : value is ;;;evaluated , it is 0 , hence ;;;(+ square 1 ) returns 1 . 1 > (defmacro square(num ) `(* ,num ,num ) ) ;;;Define a macro named square , ;;;which returns a list to be ;;;evaluated . ` is used to ;;;quote a list , while ;;;allowing variable ;;;substitution , when ;;;a variable is preceded ;;;with a comma , . SQUARE > (square 3 ) ;;;The macro square is called , ;;;it returns a list to be ;;;evaluated . ;;;The returned list evaluates ;;;the multiplication of ;;;a number by itself . ;;;the returned value is 9 . 9 > (symbol-function 1 ) ;;;1 is not a symbol , hence ;;;symbol-function will throw ;;;an error . *** - SYMBOL-FUNCTION: 1 is not a symbol The following restarts are available: ..
symbol-plist
: returns a list of properties associated with a symbol . Each property has a name , and a value , and is used to store some useful information .
symbol-plist
throws an error if the passed argument is not a symbol , and returns an empty list , if no properties are associated with a symbol.
An individual property in the property list , can be gotten using for example get
, and it can be set using for example setf
.
> (symbol-plist 'pi ) ;;;Get the value of the attribute : ;;;property list , of the symbol ;;;pi . NIL > (symbol-plist 'atom ) ;;;Get the value , of the attribute : ;;;property list , associated ;;;with the symbol atom . (SYSTEM::TYPE-SYMBOL #<SYSTEM-FUNCTION ATOM>) >(get 'atom 'SYSTEM::TYPE-SYMBOL ) ;;;Get the value of the property ;;;SYSTEM::TYPE-SYMBOL , found in ;;;the property list of the symbol ;;;atom . #<SYSTEM-FUNCTION ATOM> > (symbol-plist 1 ) ;;;Throws an error , since ;;;1 is not a symbol . *** - SYMBOL-PLIST: 1 is not a symbol The following restarts are available: .. > (symbol-plist 'search ) ;;;Get the value of the attribute : ;;;property list , of the symbol ;;;search . NIL > (get 'search 'prop ) ;;;Get the value of the ;;;property prop found in ;;;the property list of the ;;;symbol search . NIL > (get 'search 'prop 1 ) ;;;Get the value of the property , ;;;prop found in the property list ;;;of the symbol search . if no ;;;such property exists , return ;;;a value of 1 . > (setf (get 'search 'prop ) 1 ) ;;;set the value of the property , ;;;prop , in the property list ;;;of the symbol search , to 1 . > (get 'search 'prop 1 ) 1
To check if a thing is an atom , atom
can be used , and to check if a thing is a symbol , symbolp
can be used .
> (atom 1 ) ;;;t symbolizes true . t > (atom t ) t > (symbolp 1 ) ;;;nil symbolizes false . nil > (symbolp t ) t
To check if the symbol value attribute is bound , boundp
can be used , and to check if the symbol function attribute is bound , fboundp
can be used .
> (boundp 'pi ) T > (boundp 'boundp ) nil > (fboundp 'pi ) nil > (fboundp 'boundp ) t
To make the value attribute , of a symbol , unbound , makunbound
can be used , and to unbound a symbol function attribute , fmakunbound
can be used .
> (set 'img-count 3 ) ;;;Bound the value attribute ;;;of the symbol img-count ;;;with the value 3 . 3 > (boundp 'img-count ) ;;;Check if the symbol img-count ;;;is bound . t > (makunbound 'img-count ) ;;;unbound the attribute value , ;;;of the symbol img-count . IMG-COUNT > (boundp 'img-count ) ;;;Check if the symbol img-count ;;;is bound . NIL > (defun square(num ) (* num num )) ;;;Define a symbol square , and fbound ;;;it . SQUARE > (fboundp 'square ) ;;;Check if the symbol square is ;;;fbound . T > (fmakunbound 'square ) ;;;unbound the symbol square ;;;, function attribute . SQUARE > (fboundp 'square ) ;;;Check if the symbol square ;;;is fbound . NIL
What is a cons , and what is a list ?
A list is formed of zero or more cons . A cons is just an object , that holds a reference to two other objects , the first one is called a car , and the last one is called a a cdr .
In a list , the cdr of each cons is either an atom , or a reference to the next cons , and the first element of each cons , is an atom .
If the list contains no elements , it is called an empty list , and it can be noted using ()
or nil
. The empty list is both an atom , and a list , but it is not a cons .
To check if a thing is a list , listp
can be used , and to check if a thing is a cons , consp
can be used , and to check if a list is the empty list , null
can be used .
If the last element of a list is a non empty list , the list is called the dotted list .
>(atom '( ) ) t >(listp '( ) ) T >(consp '( ) ) NIL >(null '( ) ) T >(cons '1 '2 ) ;;;construct a dotted list , ;;;its last element is not ;;;nil . (1 . 2) > (cons '1 '() ) ;;;construct a list , ;;;its last element is ;;;the nil atom . (1) > (null '(1 2 ) ) ;;;(1 2 ) is not the ;;;empty list . NIL > (listp '(1 2 ) ) ;;;(1 2 ) is a list . T >(cdr '(1 . 2 ) ) ;;;Get the last element ;;;of the dotted list . 2 >(cdr '(1 ) ) ;;;Get the last element ;;;of the list . NIL