Lisp 格式和强制输出
我不明白为什么这段代码在不同的实现中表现不同:
(format t "asdf")
(setq var (read))
在 CLISP 中,它的行为与预期一致,先打印提示,然后读取,但在 SBCL 中,它读取,然后输出。我在互联网上读了一些内容并对其进行了更改:
(format t "asdf")
(force-output t)
(setq var (read))
这在 CLISP 中再次运行良好,但在 SBCL 中它仍然读取,然后输出。我什至尝试将其分离为另一个函数:
(defun output (string)
(format t string)
(force-output t))
(output "asdf")
(setq var (read))
它仍然读取,然后输出。我是否没有正确使用 force-output
或者这只是 SBCL 的一个特性?
I don't understand why this code behaves differently in different implementations:
(format t "asdf")
(setq var (read))
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))
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))
And it still reads, then outputs. Am I not using force-output
correctly or is this just an idiosyncrasy of SBCL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
FINISH-OUTPUT
。在具有缓冲输出流的系统中,某些输出保留在输出缓冲区中,直到输出缓冲区已满(然后它将自动写入目标)或输出缓冲区显式清空。
Common Lisp 为此提供了三个函数:
FINISH-OUTPUT
,尝试确保所有输出完成,然后返回。FORCE-OUTPUT
,启动剩余的输出,但立即返回并且不等待所有输出完成。CLEAR-OUTPUT
,尝试删除任何挂起的输出。不幸的是,
FORCE-OUTPUT
和FORMAT
中的T
并不相同。force-output
/finish-output
:T
是*terminal-io*
和NIL
是*标准输出*
FORMAT
: <代码>T是*标准输出*
这应该有效:
You need to use
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 has three functions for that:
FINISH-OUTPUT
, attempts to ensure that all output is done and THEN returns.FORCE-OUTPUT
, starts the remaining output, but IMMEDIATELY returns and does NOT wait for all output being done.CLEAR-OUTPUT
, tries to delete any pending output.Also the
T
inFORCE-OUTPUT
andFORMAT
are unfortunately not the same.force-output
/finish-output
:T
is*terminal-io*
andNIL
is*standard-output*
FORMAT
:T
is*standard-output*
this should work: