我希望能够从单引号中设置大小写,但这似乎是不可能的。
(format nil "The value is: ~a" 'foo)
"The value is: FOO"
(format nil "The value is: ~a" 'FOO)
"The value is: FOO"
(format nil "The value is: ~a" "Foo")
"The value is: Foo"
我希望能够从单引号中设置大小写,但这似乎是不可能的。
(format nil "The value is: ~a" 'foo)
"The value is: FOO"
(format nil "The value is: ~a" 'FOO)
"The value is: FOO"
(format nil "The value is: ~a" "Foo")
"The value is: Foo"
报价
报价与案例无关。Aquote
阻止评估。
引用一个符号:
CL-USER 1 > 'foo
FOO
引用列表:
CL-USER 2 > '(1 2 3 foo)
(1 2 3 FOO)
你可以在很多事情前面加上一个引语。例如在一个字符串前面:
CL-USER 3 > '"a b c"
"a b c"
由于字符串对自己进行评估,因此是否引用它们没有区别:
CL-USER 4 > "a b c"
"a b c"
符号默认读取为大写:
CL-USER 5 > 'FooBar
FOOBAR
CL-USER 6 > (symbol-name 'FooBar)
"FOOBAR"
但这与引用无关,是读者的特点。
CL-USER 7 > (read-from-string "foo")
FOO
3
下装
如果您希望字符串为小写,则需要将字符串转换为小写:
CL-USER 8 > (string-downcase (symbol-name 'FooBar))
"foobar"
大小写混合的符号
但是您可以创建具有小写名称或混合大小写的符号。你需要逃避它们:
CL-USER 9 > '|This is a symbol With spaces and mixed case|
|This is a symbol With spaces and mixed case|
CL-USER 10 > 'F\o\oB\a\r
|FooBar|
使用降级输出FORMAT
您还可以告诉FORMAT
以小写形式打印:
CL-USER 11 > (format nil "The value is: ~(~a~)" 'foo)
"The value is: foo"
要了解发生了什么,请参阅 Rainer Joswigs 的回答。只需添加一件事:您可以使用以下命令控制打印符号的大小写(没有竖线语法)*print-case*
:
CL-USER 1 > (let ((*print-case* :downcase))
(format nil "The value is: ~a" 'foo))
"The value is: foo"
CL-USER 2 > (let ((*print-case* :capitalize))
(format nil "The value is: ~a" 'foo))
"The value is: Foo"
CL-USER 3 > (let ((*print-case* :upcase)) ; default
(format nil "The value is: ~a" 'foo))
"The value is: FOO"
'foo
意思是“抑制符号 FOO 的评估,只留下符号 FOO”。默认情况下,Common Lisp 倾向于向上转换符号名称(因此表示为 的符号'foo
和'Foo
都是'FOO
相同的符号,带有符号名称"FOO"
)。
要确切了解您的实现将做什么,您可以检查当前 readtable 的 readtable case,请参阅 CLHS,ch 23.1.2,通过调用readtable case 的效果(readtabe-case *readtable*)
。
一些 lisp 实现将从 readtable-case as 开始:preserve
。
至于您是否应该使用符号或字符串,它是“取决于”的其中之一。如果您不担心 cse 保存,则使用 interned 符号可以减少存储空间并加快比较速度,而代价是大小写处理的(可能)价格。但是,如果大小写很重要,那么平衡可能会更接近规模的“始终使用字符串”端。
Lisp 符号,如'a
,不区分大小写。您可以通过以下方式检查...
(eq 'a 'A)
它们是同一个符号。
如果需要区分大小写,则应酌情使用字符串或字符。
为了format
在特定情况下打印符号,您可以将*print-case*
全局变量设置为:upcase
,:downcase
或:capitalize
根据您的需要。
好的,这有效:
(format nil "The value is: ~a" (string-downcase 'foo))
"The value is: foo"
更好(来自雷纳)
(format nil "The value is: ~(~a~)" 'foo)
如果意图是表示一个字符串,我仍然认为不应该使用 'foo 而不是“foo”。