Search code examples
iocommon-lispsbclclisp

sbcl - why not execute code in sequence


I'm excising example from book: ANSI Common Lisp, charpter 14.6 conditions.

sbcl not print the prompt in correct order:

example code:

(defun user-input (prompt)
    (format t prompt)
    (let ((str (read-line)))
      (or (ignore-errors (read-from-string str))
      nil)))

In sbcl test, the prompt will not print in order:

* (defun user-input (prompt)
    (format t prompt)
    (let ((str (read-line)))
      (or (ignore-errors (read-from-string str))
      nil)))

STYLE-WARNING: redefining COMMON-LISP-USER::USER-INPUT in DEFUN

USER-INPUT
* (user-input "Please type an expression> ")
test
Please type an expression> 
TEST
* (user-input "Please type an expression> ")
#%@#+!!
Please type an expression> 
NIL
* 

But in clisp, the prompt is printed in order and it works as expected:

[5]> (defun user-input (prompt)
    (format t prompt)
    (let ((str (read-line)))
      (or (ignore-errors (read-from-string str))
You are in the top-level Read-Eval-Print loop.
Help (abbreviated :h) = this list
Use the usual editing capabilities.
(quit) or (exit) leaves CLISP.
  nil)))
USER-INPUT
[6]> (user-input "Please type an expression> ")
Please type an expression> #%@#+!!
NIL
[7]> 

Solution

  • Insert force-output after format in your user-input to avoid i/o buffering issues: normally, nothing is actually printed to the device until a newline or force-output or finish-output.