2

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?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Martin Janiczek
  • 2,996
  • 3
  • 24
  • 32

1 Answers1

9

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.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • Aha, buffering! Thanks, adding (finish-output) after the (format ...) line solves this problem. – Martin Janiczek Feb 14 '12 at 12:22
  • 1
    Also, you were right about the duplicates. I didn't check similar questions - my bad. http://stackoverflow.com/questions/3426312/when-does-format-actually-print-in-common-lisp – Martin Janiczek Feb 14 '12 at 12:25