Search code examples
lispcommon-lispsbclslime

READ runs before FORMAT


Ran this in SLIME and wondering why it waits on read input before outputting format.

(defun wage ()
  (format t "~&Enter wage: ")
  (let ((wage (read)))
    (format t "~&Enter hours: ")
    (let ((hours (read)))
      (format t "~&Earned ~S dollars." (* wage hours)))))

* (wage)

2
Enter wage: 
3
Enter hours: 
Earned 6 dollars.
NIL

Solution

  • That happens because the standard output stream is buffered, which means that things that are printed to it do not actually write to the display straight away. You need to call (finish-output) before (read) in each instance to ensure that anything that's been buffered is written first.