📜  LISP 中的属性列表

📅  最后修改于: 2022-05-13 01:55:43.493000             🧑  作者: Mango

LISP 中的属性列表

在 Lisp 中,每个符号都有一个属性列表( plist )。最初创建符号时,其属性列表为空。属性列表由条目组成,其中每个条目都包含一个称为指标的键和一个称为属性的值。指标间无重复。与关联列表相反,添加和删除plist条目的操作会更改plist而不是创建新的。

               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.

例子:

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))


输出:

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