1
votes

I'm going through the book ANSI Common Lisp by Paul Graham, and there's this example:

(defun ask-number ()
  (format t "Please enter a number. ")
  (let ((val (read)))
       (if (numberp val)
           val
           (ask-number))))

It should behave like this:


$ (ask-number)

Please enter a number. a

Please enter a number. (ho hum)

Please enter a number. 52

52


But when I try it (SBCL 1.0.55), it doesn't print the format string until successful read:


$ (ask-number)

a

(ho hum)

52

Please enter a number. Please enter a number. Please enter a number.

52


Where's the error? How to make it behave the intended way?

1

1 Answers

8
votes

This is an often asked question. There are possibly duplicates of this on Stackoverflow.

The output can be buffered.

You then need to call the standard Common Lisp function FINISH-OUTPUT to force the IO system to write any pending output.

After that, read.