views:

39

answers:

1

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?

+8  A: 

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.

Heath Hunnicutt
Right, implementations may use buffered IO streams and don't need to force output after a format or before a read. So for portable code one has to call something like force-output or finish-output.
Rainer Joswig