English 中文(简体)
LISP - Symbols
  • 时间:2024-09-17

LISP - Symbols


Previous Page Next Page  

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 pst, or ppst.

Property Lists

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

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

When a symbol is created, its property pst 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 pubpsher, and respective values, to an object named (symbol) book .

Example 1

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

(write (setf (get  books title)  (Gone with the Wind)))
(terpri)
(write (setf (get  books  author)  (Margaret Michel)))
(terpri)
(write (setf (get  books  pubpsher)  (Warner Books)))

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

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

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

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

get symbol indicator &optional default

The get function looks for the property pst 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 2

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

(setf (get  books  title)  (Gone with the Wind))
(setf (get  books  author)  (Margaret Micheal))
(setf (get  books  pubpsher)  (Warner Books))

(write (get  books  title))
(terpri)
(write (get  books  author))
(terpri)
(write (get  books  pubpsher))

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

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

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

Example 3

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

(setf (get  annie  age) 43)
(setf (get  annie  job)  accountant)
(setf (get  annie  sex)  female)
(setf (get  annie  children) 3)

(terpri)
(write (symbol-ppst  annie))

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 4

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

(setf (get  annie  age) 43)
(setf (get  annie  job)  accountant)
(setf (get  annie  sex)  female)
(setf (get  annie  children) 3)

(terpri)
(write (symbol-ppst  annie))
(remprop  annie  age)
(terpri)
(write (symbol-ppst  annie))

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