gpt4 book ai didi

format - 为什么 Lisp 中的单引号总是返回大写字母?

转载 作者:行者123 更新时间:2023-12-01 22:51:29 32 4
gpt4 key购买 nike

我希望能够从单引号设置大小写,但这似乎不可能。

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

最佳答案

引用

引用内容与大小写无关。 引用可以防止评估。

引用一个符号:

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"

关于format - 为什么 Lisp 中的单引号总是返回大写字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31943628/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com