引用用管道围绕其输出 - 为什么?

时间:2015-04-08 16:03:27

标签: lisp common-lisp

我创建了一个表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)))

现在,我想通过运行mapcar来提取社会安全号码,如下所示:

(mapcar #'caddr daily-planet)

结果是:

(|123-76-4535| |089-52-6787| |951-26-1438| |355-16-7439|)

我原以为:

(123-76-4535 089-52-6787 951-26-1438 355-16-7439)

当我尝试运行更简单的

'(123-456)

我已经看到它也被评估为:

(|123-456|)

为什么?

1 个答案:

答案 0 :(得分:5)

与评估无关。

CL-USER 84 > '123-456-789
|123-456-789|

CL-USER 85 > (type-of '123-456-789)
SYMBOL

123-456-789是一个符号。打印机可能会将其打印为|123-456-789|.原因:它只有数字和名称中的-字符,这使得打印的表示形式成为所谓的潜在数字。 / p>

管道符是符号的转义字符。它包围符号字符串,它将是符号名称。

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 --- !!!"

出于某种原因,打印机可能会认为123-456-789应该打印出来。但符号名称是相同的:

CL-USER 88 > (eq '123-456 '|123-456|)
T

转义允许符号具有任意名称。

一个无用的例子:

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)

反斜杠是单个转义(默认情况下字符将被提升):

CL-USER 93 > 'foo\foo\ bar
|FOOfOO BAR|

旁注

Common Lisp有潜在数字的想法。各种数字类型有一个数字语法:整数,浮点数,比率,复数......但是标准定义的扩展空间。例如123-456-789可能在扩展的Common Lisp中有一个数字!当打印机看到一个看起来可能是潜在数字的符号时,它会逃脱符号,以便它可以安全地回读为符号......