Lisp - Symbols



In LISP, a symbol is a name that represents data objects and interestingly it is also a data object.

What makes symbols special is that they have a component called the property list, or plist.

Property Lists

LISP allows you to assign properties to symbols. For example, let us have a 'person' object. We would like this 'person' object to have properties like name, sex, height, weight, address, profession etc. A property is like an attribute name.

A property list is implemented as a list with an even number (possibly zero) of elements. Each pair of elements in the list constitutes an entry; the first item is the indicator, and the second is the value.

When a symbol is created, its property list is initially empty. Properties are created by using get within a setf form.

For example, the following statements allow us to assign properties title, author and publisher, and respective values, to an object named (symbol) 'book'.

Example

Create a new source code file named main.lisp and type the following code in it.

main.lisp

; create object books, assign symbol title a value
(write (setf (get 'books'title) '(Gone with the Wind)))
; terminate printing
(terpri)
; update object books, assign symbol author a value
(write (setf (get 'books 'author) '(Margaret Michel)))
; terminate printing
(terpri)
; update object books, assign symbol publisher a value
(write (setf (get 'books 'publisher) '(Warner Books)))

Output

When you execute the code, it returns the following result −

(GONE WITH THE WIND)
(MARGARET MICHEL)
(WARNER BOOKS)

Various property list functions allow you to assign properties as well as retrieve, replace or remove the properties of a symbol.

The get function returns the property list of symbol for a given indicator. It has the following syntax −

get symbol indicator &optional default

The get function looks for the property list of the given symbol for the specified indicator, if found then it returns the corresponding value; otherwise default is returned (or nil, if a default value is not specified).

Example

Update the source code file named main.lisp and type the following code in it.

main.lisp

; create object books, assign symbol title a value
(setf (get 'books 'title) '(Gone with the Wind))
; create object books, assign symbol author a value
(setf (get 'books 'author) '(Margaret Micheal))
; create object books, assign symbol publisher a value
(setf (get 'books 'publisher) '(Warner Books))

; retrieve title from books object
(write (get 'books 'title))
; terminate printing
(terpri)
; retrieve author from books object
(write (get 'books 'author))
; terminate printing
(terpri)
; retrieve publisher from books object
(write (get 'books 'publisher))

Output

When you execute the code, it returns the following result −

(GONE WITH THE WIND)
(MARGARET MICHEAL)
(WARNER BOOKS)

The symbol-plist function allows you to see all the properties of a symbol.

Example

Create a new source code file named main.lisp and type the following code in it.

main.lisp

; create object annie, assign symbols values
(setf (get 'annie 'age) 43)
(setf (get 'annie 'job) 'accountant)
(setf (get 'annie 'sex) 'female)
(setf (get 'annie 'children) 3)
; terminate printing
(terpri)
; print symbol lists
(write (symbol-plist 'annie))

Output

When you execute the code, it returns the following result −

(CHILDREN 3 SEX FEMALE JOB ACCOUNTANT AGE 43)

The remprop function removes the specified property from a symbol.

Example

Update the source code file named main.lisp and type the following code in it.

main.lisp

; create object annie, assign symbols values
(setf (get 'annie 'age) 43)
(setf (get 'annie 'job) 'accountant)
(setf (get 'annie 'sex) 'female)
(setf (get 'annie 'children) 3)
; terminate printing
(terpri)
; print symbol lists
(write (symbol-plist 'annie))
; removing properties age
(remprop 'annie 'age)
; terminate printing
(terpri)
; print symbol lists
(write (symbol-plist 'annie))

Output

When you execute the code, it returns the following result −

(CHILDREN 3 SEX FEMALE JOB ACCOUNTANT AGE 43)
(CHILDREN 3 SEX FEMALE JOB ACCOUNTANT)
Advertisements