📜  LISP 中的符号

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

LISP 中的符号

符号是 lisp 数据对象,每种类型的符号对象都有一个称为打印名称的名称。

符号名称可以包含字母和数字的任意组合,以及一些特殊字符,例如连字符。符号可以包含任何字母、数字或任何字符,但分隔字符(如括号或空格)除外。

符号示例:

Banana
age 
year-of-birth 
123%$ 
/home/user/work 
b^2-a*c

整数和符号的区别:

   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 .

特殊符号 T 和 NIL:

  • T:真相,“是的”
  • NIL:错误,“不”

某些称为谓词的 lisp 函数用 T 和 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

一些常见的 lisp 约定:

如果由于小写字母或名称中的特殊字符而导致符号出现问题,则存在转义约定。

  • 在任何字符之前写一个“/”字符会导致该字符本身被视为一个普通字符,用于符号名称;特别是,它抑制了小写字母到大写的内部转换。
5.6789/p0 : 5.6789p0 is 1 symbol.
5.6789/P0 : 5.6789P0 is another  symbol.
  • 竖条中符号的周围名称。
|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 ||.) 

符号的属性:

在 lisp 中,可以将属性分配给符号。

For example : The symbol dog can have properties like colour , weight , breed.

这是在属性列表或plist的帮助下完成的。在 Lisp 中,每个符号都有一个属性列表(plist)。最初创建符号时,其属性列表为空。属性列表由条目组成,其中每个条目都由一个称为指标的键和一个值组成。指标间无重复。

与属性列表相关的一些常用功能:

  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


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


输出:

ANNA
(SIBLING ANNA AGE 20)

在上面的示例中,hritik 是一个符号和年龄,兄弟姐妹是分配给它的属性(指标),具有值 20 和 Anna。

语言

(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

输出:

(BREED DALMATIAN NAME TOM)
(NAME TOM)

在上面的示例中,使用remprop函数删除了符号 dog 的属性品种。