Skip to content
Related Articles

Related Articles

Property Lists in LISP

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 31 Jan, 2022

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 called a property . There are no duplicates among the indicators. In contrast to association lists the operations for adding and removing plist entries alter the plist rather than creating a new one.

               Function                   

SyntaxUsage
get functionget symbol indicator &optional defaultget searched 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)  property/value)setf is used with getting to create a new property value pair.
symbol-plist(symbol-plist  symbol)symbol-plist allows you to see all the properties of a symbol
rempropremprop symbol indicatorremprop function is used to remove the property equivalent to the indicator.
getfgetf place indicator &optional defaultgetf searches the property list stored in place for an indicator equivalent to an indicator. If one is found, then the corresponding value is returned; otherwise, the default is returned. If the default is not specified, then nil is used for default.
remfremf place indicatorremf removes the property equivalent to the given indicator from the given place.

Example:

Lisp




(setf (get 'aastha 'age ) 20)
;using setf (which understands get as a place description) 
;to create indicator age with value 20 of symbol aastha.
  
(setf (get 'aastha 'occupation ) 'doctor)
;using setf (which understands get as a place description) 
;to create indicator occupation with value doctor of symbol aastha.
  
(setf (get 'aastha 'fav-book ) 'harrypotter)
;using setf (which understands get as a place description)
;to create indicator fav-book with value harrypotter of symbol aastha.
  
(write (symbol-plist 'aastha))
;using symbol-plist to return aastha(symbol's) plist .
  
(terpri)
(remprop 'aastha 'fav-book)
;using remprop to remove property of symbol aastha
;which has indicator fav-book
  
(write (symbol-plist 'aastha))
(terpri)
(setq x '())
(setf (getf x 'height ) 20)
  
;using setf along with getf to set property of
;indicator height as 20 at place x
(write(eq(getf x 'height) 20))
  
;using eq to check if property of
;indicator height at place x is 20
(terpri)
(remf x 'height)
  
;using remf to remove property 
;of indicator height at place x
(write(eq(getf x 'height) 20))

Output:

(FAV-BOOK HARRYPOTTER OCCUPATION DOCTOR AGE 20)
(OCCUPATION DOCTOR AGE 20)
T
NIL

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!