Open In App

Property Lists in LISP

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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                   

Syntax Usage
get function get symbol indicator &optional default get 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
remprop remprop symbol indicator remprop function is used to remove the property equivalent to the indicator.
getf getf place indicator &optional default getf 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.
remf remf place indicator remf 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


Last Updated : 31 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads