- LISP - CLOS
- LISP - Error Handling
- LISP - Packages
- LISP - Structures
- LISP - File I/O
- LISP - Input & Output
- LISP - Hash Table
- LISP - Tree
- LISP - Set
- LISP - Vectors
- LISP - Symbols
- LISP - Lists
- LISP - Sequences
- LISP - Strings
- LISP - Arrays
- LISP - Characters
- LISP - Numbers
- LISP - Predicates
- LISP - Functions
- LISP - Loops
- LISP - Decisions
- LISP - Operators
- LISP - Constants
- LISP - Variables
- LISP - Macros
- LISP - Data Types
- LISP - Basic Syntax
- LISP - Program Structure
- LISP - Environment
- LISP - Overview
- LISP - Home
LISP Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 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