我在使用 Common Lisp 的 format
指令时遇到了一个奇怪的问题,它只在 GNU CLISP 中出现,这让我怀疑它是否是实现中的错误。
考虑以下代码
(defun f (s args)
(format nil "~A~{~A~%~}" s args))
函数 f
生成一个字符串,该字符串由 s
参数组成,后跟每个参数,其中每个参数(但不是标题)后跟一个换行符。所以,例如,我希望
(format t "~A" (f "foo" '("arg1" "arg2")))
生产
fooarg1
arg2
它所做的,正确的。现在,考虑以下调用
(format t "~A" (f "a" ()))
(format t "~A" (f "b" ()))
(format t "~A" (f "c" '("d")))
鉴于我曾经打印出的唯一换行符是 f
的第二个参数的后续元素,并且前两个调用为第二个 f
参数传递了一个空列表,我希望直到最后都不会看到换行符,即我希望打印出来
abcd
它在 SBCL 中正是这样做的。但是,在 GNU CLISP 中,我得到了
ab
cd
注意 b
和 c
之间的换行符。
将代码更改为
(format t "~S" (f "a" ()))
(format t "~S" (f "b" ()))
(format t "~S" (f "c" '("d")))
显示以下结果,甚至不那么有启发性
"a""b"
"cd
"
因此 b
和 c
之间的换行符不属于任何一个字符串。为什么 CLISP 决定在这些 format
语句之间打印一个换行符?我怎样才能阻止它这样做?
最佳答案
这是 GNU CLISP 的一个特性。查看documentation of *pprint-first-newline*
.
如果你想要一个不同的输出,绑定(bind) *print-pretty*
或 *pprint-first-newline*
在你的format
期间为零调用。
关于format - Common Lisp格式指令中出现神秘的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67845195/