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

标签 format common-lisp string-formatting clisp

我在使用 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

注意 bc 之间的换行符。

将代码更改为

(format t "~S" (f "a" ()))
(format t "~S" (f "b" ()))
(format t "~S" (f "c" '("d")))

显示以下结果,甚至不那么有启发性

"a""b"
"cd
"

因此 bc 之间的换行符不属于任何一个字符串。为什么 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/

相关文章:

python - 串联字符串的字符串格式?

Nginx 日志格式 - 删除变量后面的空格

templates - 转到模板 : Currency pipe format?

function - Lisp 标签函数在使用前被删除

lisp - (loop for) 和 (loop :for) in Common Lisp 之间的区别

c# - 将字符串格式化为 10 个字符

c - sprintf 格式 - u 之前有三个 '%%%',n 之前有两个 '%%'?

android - 在 Android 中格式化多行 TextView 的第一行

Java System.out.print 格式化

common-lisp - 普通口齿不清 : How many argument can a function take?