When does format actually print in Common Lisp?

2019-06-20 18:24发布

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?

1条回答
地球回转人心会变
2楼-- · 2019-06-20 18:45

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.

查看更多
登录 后发表回答