Lisp 格式和强制输出

发布于 2024-08-18 06:30:07 字数 520 浏览 2 评论 0原文

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

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2024-08-25 06:30:07

您需要使用FINISH-OUTPUT

在具有缓冲输出流的系统中,某些输出保留在输出缓冲区中,直到输出缓冲区已满(然后它将自动写入目标)或输出缓冲区显式清空。

Common Lisp 为此提供了三个函数:

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

  • FORCE-OUTPUT,启动剩余的输出,但立即返回并且不等待所有输出完成。

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

不幸的是,FORCE-OUTPUTFORMAT 中的 T 并不相同。

  • force-output / finish-outputT*terminal-io*NIL*标准输出*

  • FORMAT: <代码>T是*标准输出*

这应该有效:

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

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 in FORCE-OUTPUT and FORMAT are unfortunately not the same.

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

  • FORMAT: T is *standard-output*

this should work:

(format t "asdf")
(finish-output nil)   ;  note the NIL
(setq var (read))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文