Symbols are lisp data objects and every type of symbol object has a name called its print name.
Symbol names may contain any combination of letters and numbers, plus some special characters such as hyphens. A symbol can contain any alphabetic, numeric, or any characters except delimiter characters like parenthesis or space.
Examples of symbols :
Banana
age
year-of-birth
123%$
/home/user/work
b^2-a*c
Difference between integers and symbols:
| Integers |
Symbols
|
|
A sequence of numbers from 0 to 9.
eg: +7 and 7 are integers.
|
A sequence of letters, digits and, other permissible characters.
eg: + , – , * are all symbols .
|
Special symbols T and NIL:
- T : Truth, ” yes “
- NIL : False, “no”
Certain lisp functions called predicates answer questions with T and NIL.
Note:
abcDEf
ABCDEF
ABCdef
Are all the same symbol.
Lisp reader converts lowercase letters to corresponding uppercase letters while reading symbols so case makes
no difference while notating a symbol
Some common lisp conventions :
If there are problems in notating a symbol due to lowercase letters or special characters in its name there are escape conventions.
- Writing a ‘/’ character before any character causes the character to be treated itself as an ordinary character for use in a symbol name; in particular, it suppresses the internal conversion of lowercase letters to uppercase.
5.6789/p0 : 5.6789p0 is 1 symbol.
5.6789/P0 : 5.6789P0 is another symbol.
- Surrounding name of the symbol in the vertical bar.
|h^2 - 2gt| : h^2 - 2gt is a symbol .
(As visible dilimiter like spaces can also be used in the symbol name by
surrounding it within ||.)
Properties of Symbols:
In lisp, properties can be assigned to symbols.
For example : The symbol dog can have properties like colour , weight , breed.
This is done with the help of a property list or plist. In Lisp, every symbol has a property list (plist). When a symbol is created initially its property list is empty. A property list consists of entries where every entry consists of a key called an indicator and a value. There are no duplicates among the indicators.
Some common functions related to the property list:
| Function |
Syntax |
Usage |
| get function |
get symbol indicator &optional default |
get searches the plist for an indicator equivalent to indicator. If the found value is returned or else the default is returned. If the default is not specified nil is returned |
| setf function |
setf((get function) value) |
The setf is used with get to create a new indicator value pair. |
| symbol-plist |
(symbol-plist symbol) |
The symbol-plist allows you to see all the properties of a symbol |
| remprop |
remprop symbol indicator |
The remprop function is used to remove the property equivalent to the indicator. |
Lisp
(setf (get 'hritik 'age) '20)
;using setf function along with get to create an
;indicator age with value 20 of symbol hritik
(setf (get 'hritik 'sibling) 'Anna)
;using setf function along with get to create
;an indicator sibling with value Anna of symbol hritik
(write (get 'hritik 'sibling))
;using get function to give the property list of
;symbol hritik for the indicator sibling
(terpri)
(write (symbol-plist 'hritik))
;using symbol-plist function to return plist of symbol hritik
|
Output:
ANNA
(SIBLING ANNA AGE 20)
In the above example, hritik is a symbol and age, siblings are properties(indicators) assigned to it having values 20 and Anna.
Lisp
(setf (get 'dog 'name) 'tom)
;using setf function along with get to create an indicator
;name with value tom of symbol dog
(setf (get 'dog 'breed) 'dalmatian)
;using setf function along with get to create an
;indicator breed with value dalmatian of symbol dog
(write (symbol-plist 'dog))
;using symbol-plist function to return plist of symbol dog
(remprop 'dog 'breed)
(terpri)
;using remprop to remove the property breed of symbol dog
(write (symbol-plist 'dog))
;using symbol-plist function to return plist of symbol dog
|
Output:
(BREED DALMATIAN NAME TOM)
(NAME TOM)
In the above example the property breed of symbol dog is removed using the remprop function.
Similar Reads
Symbols in LISP
Symbols are lisp data objects and every type of symbol object has a name called its print name. Symbol names may contain any combination of letters and numbers, plus some special characters such as hyphens. A symbol can contain any alphabetic, numeric, or any characters except delimiter characters l
4 min read
Analysis Symbols
In mathematics, Analysis Symbols are graphical representations and notations used to describe mathematical processes, relationships, and concepts in the field of mathematical analysis. They function as a symbolic language, allowing mathematicians to express concepts precisely and clearly. Analysis S
10 min read
Macros in LISP
Macro is similar to a function in popular languages like C++, java, etc. that takes arguments and returns a LISP form to be evaluated. It is useful when the same code has to be executed with a few variable changes.For example, rather than writing the same code for squaring a number, we can define a
2 min read
Sets in LISP
A set is an unordered collection of items. A set is just like other data structures. In C++ we have a similar data structure called a hash map. Common lisp does not provide a built-in set data type, but it provides a number of functions that allow set operations to be performed onto a list. Using th
7 min read
Numbers in LISP
LISP is a list processing programming language. It is widely used in the manipulation of data strings. It provides an input and output library. LISP provides a macro system and provides well control structures for the manipulation of data. LISP Math Function:floor: floor returns the nearest smallest
2 min read
Logic symbols
Logic symbols are the symbols used to represent logic in mathematics. There are multiple logic symbols including quantifiers, connectives and other symbols. In this article we will explore all the logic symbols that are useful to represent logical statements in mathematical form. Let's start our lea
5 min read
Lists in LISP
Lists in common LISP is simply a single Linked list. In LISP, Lists are designed as a chain of records. While talking about record structures in LISP, the concept of Cons is vital. Cons in LISP is a record structure with 2 primary components. A cons function takes in 2 arguments and returns a new co
2 min read
Set Symbols
Set Symbols are a collective term used for all the symbols used in set theory which is the branch of mathematics that deals with the collection of objects and their various properties. A set is a well-defined collection of objects where each object in the collection is called an element and each ele
15 min read
Variables in LISP
Similar to other languages, in LISP variables are named places that store a particular value, but they are not declared the way you declare variables in C++ or Java i.e you don't need to mention data-type of variables when declaring them as LISP is dynamically typed. LISP supports two types of varia
3 min read
Vectors in LISP
In this article, we will discuss Vectors in LISP. Vectors in LISP are one-dimensional arrays which are also known as sequences. We can create a vector using vector function and # symbol Syntax: variable_name(vector element1 element2 ... element n) or variable_name #(element1 element2 ... element n)
2 min read