神秘的换行符出现在 Common Lisp 格式指令中

Sil*_*olo 3 format clisp common-lisp string-formatting

我遇到了一个关于 Common Lispformat指令的奇怪问题,它只在 GNU CLISP 中显示,这让我怀疑它是否是实现中的错误。

考虑以下代码

(defun f (s args)
  (format nil "~A~{~A~%~}" s args))
Run Code Online (Sandbox Code Playgroud)

该函数f生成一个由s参数组成的字符串,后跟每个参数,其中每个参数(但不是标题)后跟一个换行符。所以,例如,我希望

(format t "~A" (f "foo" '("arg1" "arg2")))
Run Code Online (Sandbox Code Playgroud)

生产

fooarg1
arg2
Run Code Online (Sandbox Code Playgroud)

它确实做到了。现在,考虑以下调用

(format t "~A" (f "a" ()))
(format t "~A" (f "b" ()))
(format t "~A" (f "c" '("d")))
Run Code Online (Sandbox Code Playgroud)

鉴于我打印出的唯一换行符是来自 的第二个参数的元素f,并且前两次调用为第二个f参数传递一个空列表,我希望直到最后都看不到换行符,即我希望它打印

abcd
Run Code Online (Sandbox Code Playgroud)

它在 SBCL 中正是这样做的。但是,在 GNU CLISP 中,我得到

ab
cd
Run Code Online (Sandbox Code Playgroud)

需要注意的换行符bc

将代码更改为

(format t "~S" (f "a" ()))
(format t "~S" (f "b" ()))
(format t "~S" (f "c" '("d")))
Run Code Online (Sandbox Code Playgroud)

显示以下,甚至更不具启发性的结果

"a""b"
"cd
"
Run Code Online (Sandbox Code Playgroud)

所以之间的换行b,并c没有任字符串的一部分。为什么 CLISP 决定在这些format语句之间打印换行符?我怎样才能防止它这样做?

Bru*_*ble 6

这是 GNU CLISP 的一个特性。请参阅 的文档*pprint-first-newline*

如果您想要不同的输出,请在调用期间将*print-pretty*或绑定*pprint-first-newline*到 nil format