Skip to content
Related Articles

Related Articles

Symbols in LISP

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 08 Feb, 2022

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 functionget symbol indicator &optional defaultget 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 functionsetf((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
rempropremprop symbol indicatorThe 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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!