Common Lisp 中的 format 何时真正打印?

发布于 2024-09-13 09:21:44 字数 410 浏览 4 评论 0原文

我有以下 Common Lisp 代码:

(defun micro-read-eval-print ()
    (format t "Micro > ")
    (let ((form (read-line)))))

当我运行它时,我得到以下信息:

CL-USER> (micro-read-eval-print)
(m-quote a)
Micro > NIL

请注意,我输入了“(m-quote a)”,而 Lisp 解释器输出“Micro > NIL”。

现在,我预计这些事件会以相反的顺序发生。我本来希望“Micro >”首先被打印,因为格式声明首先出现。为什么不先打印出来?我该怎么做才能确保它首先被打印出来?

I have the following Common Lisp code:

(defun micro-read-eval-print ()
    (format t "Micro > ")
    (let ((form (read-line)))))

When I run it, I get the following:

CL-USER> (micro-read-eval-print)
(m-quote a)
Micro > NIL

Note that I typed in "(m-quote a)", while the Lisp interpreter output "Micro > NIL".

Now, I would have expected these events to happen in the reverse order. I would have expected "Micro > " to have been printed first since the format statement comes first. Why isn't it printed first? And what do I have to do to make sure it is printed first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

永不分离 2024-09-20 09:21:44

尝试添加

(defun micro-read-eval-print ()
    (format t "Micro > ")
    (finish-output)
    (let ((form (read-line)))))

我相信您遇到了标准 io (stdio) 的缓冲,在 C 语言中,通常通过该语言中的 fflush() 绕过该缓冲。

finish-output 似乎是 Common Lisp 中 C 标准库的 fflush 的等价物。

Try adding

(defun micro-read-eval-print ()
    (format t "Micro > ")
    (finish-output)
    (let ((form (read-line)))))

I believe you are encountering the buffering of standard io (stdio) which, in C, is commonly bypassed via fflush() in that language.

finish-output appears to be the Common Lisp equivalent of C standard library's fflush.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文