我创建了一个表daily-planet
如下:
(setf daily-planet '((olsen jimmy 123-76-4535 cub-reporter)
(kent clark 089-52-6787 reporter)
(lane lois 951-26-1438 reporter)
(white perry 355-16-7439 editor)))
Run Code Online (Sandbox Code Playgroud)
现在我想通过运行mapcar
如下提取社会安全号码:
(mapcar #'caddr daily-planet)
Run Code Online (Sandbox Code Playgroud)
结果是:
(|123-76-4535| |089-52-6787| |951-26-1438| |355-16-7439|)
Run Code Online (Sandbox Code Playgroud)
我原以为:
(123-76-4535 089-52-6787 951-26-1438 355-16-7439)
Run Code Online (Sandbox Code Playgroud)
当我试图运行更简单
'(123-456)
Run Code Online (Sandbox Code Playgroud)
我已经看到它也被评估为:
(|123-456|)
Run Code Online (Sandbox Code Playgroud)
为什么?
它与评估无关.
CL-USER 84 > '123-456-789
|123-456-789|
CL-USER 85 > (type-of '123-456-789)
SYMBOL
Run Code Online (Sandbox Code Playgroud)
123-456-789
是一个象征.打印机可能会将其打印为|123-456-789|.
原因:它只有数字和-
名称中的字符,这使得打印的表示形式成为所谓的潜在数字.
管道字符是符号的转义字符.它包围符号字符串,这将是符号名称.
CL-USER 86 > '|This is a strange symbol with digits, spaces and other characters --- !!!|
|This is a strange symbol with digits, spaces and other characters --- !!!|
CL-USER 87 > (symbol-name *)
"This is a strange symbol with digits, spaces and other characters --- !!!"
Run Code Online (Sandbox Code Playgroud)
由于某种原因,打印机可能认为123-456-789
应该打印转义.但符号名称是相同的:
CL-USER 88 > (eq '123-456 '|123-456|)
T
Run Code Online (Sandbox Code Playgroud)
转义允许符号具有任意名称.
一个无用的例子:
CL-USER 89 > (defun |Gravitation nach Einstein| (|Masse in kg|) (list |Masse in kg|))
|Gravitation nach Einstein|
CL-USER 90 > (|Gravitation nach Einstein| 10)
(10)
Run Code Online (Sandbox Code Playgroud)
反斜杠是单个转义(默认情况下字符将被提升):
CL-USER 93 > 'foo\foo\ bar
|FOOfOO BAR|
Run Code Online (Sandbox Code Playgroud)
旁注:
Common Lisp有潜在数字的想法.各种数字类型有一个数字语法:整数,浮点数,比率,复数......但是标准定义的扩展空间.例如123-456-789可能在扩展的Common Lisp中有一个数字!当打印机看到一个看起来可能是潜在数字的符号时,它会逃脱符号,这样它就可以安全地回读为符号......