Paul Reiners
Paul Reiners

Reputation: 7894

When does format actually print in Common Lisp?

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?

Upvotes: 6

Views: 347

Answers (1)

Heath Hunnicutt
Heath Hunnicutt

Reputation: 19467

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.

Upvotes: 11

Related Questions