Lisp 格式和强制输出 [英] Lisp format and force-output

查看:25
本文介绍了Lisp 格式和强制输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么这段代码在不同的实现中表现不同:

I don't understand why this code behaves differently in different implementations:

(format t "asdf")
(setq var (read))

在 CLISP 中,它的行为符合预期,在读取后打印提示,但在 SBCL 中它读取,然后输出.我在网上看了一点,改了一下:

In CLISP it behaves as would be expected, with the prompt printed followed by the read, but in SBCL it reads, then outputs. I read a bit on the internet and changed it:

(format t "asdf")
(force-output t)
(setq var (read))

这再次在 CLISP 中工作正常,但在 SBCL 中它仍然读取,然后输出.我什至尝试将其拆分为另一个函数:

This, again, works fine in CLISP, but in SBCL it still reads, then outputs. I even tried separating it into another function:

(defun output (string)
   (format t string)
   (force-output t))
(output "asdf")
(setq var (read))

它仍然读取,然后输出.是我没有正确使用 force-output 还是这只是 SBCL 的一个特性?

And it still reads, then outputs. Am I not using force-output correctly or is this just an idiosyncrasy of SBCL?

推荐答案

需要使用FINISH-OUTPUT.

在具有缓冲输出流的系统中,一些输出保留在输出缓冲区中,直到输出缓冲区已满(然后它会自动写入目标)或输出缓冲区被明确清空.

In systems with buffered output streams, some output remains in the output buffer until the output buffer is full (then it will be automatically written to the destination) or the output buffer is explicity emptied.

Common Lisp 为此提供了三个功能:

Common Lisp has three functions for that:

  • FINISH-OUTPUT,尝试确保所有输出完成,然后返回.

  • FINISH-OUTPUT, attempts to ensure that all output is done and THEN returns.

FORCE-OUTPUT,开始剩余的输出,但立即返回并且不等待所有输出完成.

FORCE-OUTPUT, starts the remaining output, but IMMEDIATELY returns and does NOT wait for all output being done.

CLEAR-OUTPUT,尝试删除任何挂起的输出.

CLEAR-OUTPUT, tries to delete any pending output.

不幸的是,FORCE-OUTPUTFORMAT 中的 T 也不一样.

Also the T in FORCE-OUTPUT and FORMAT are unfortunately not the same.

  • force-output/finish-output:T*terminal-io*NIL*standard-output*

FORMAT:T*standard-output*

这应该有效:

(format t "asdf")
(finish-output nil)   ;  note the NIL
(setq var (read))

这篇关于Lisp 格式和强制输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆