【问题标题】:Mysterious newline appears in Common Lisp format directiveCommon Lisp 格式指令中出现神秘的换行符
【发布时间】:2021-08-22 23:28:22
【问题描述】:

我在 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 语句之间打印一个换行符?我怎样才能阻止它这样做?

【问题讨论】:

    标签: format common-lisp string-formatting clisp


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-27
      相关资源
      最近更新 更多