Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

io - Lisp format and force-output

I don't understand why this code behaves differently in different implementations:

(format t "asdf")
(setq var (read))

In CLISP it behaves as would be expected, with the prompt printed followed by the read, but in SBCL it reads, then outputs. I read a bit on the internet and changed it:

(format t "asdf")
(force-output t)
(setq var (read))

This, again, works fine in CLISP, but in SBCL it still reads, then outputs. I even tried separating it into another function:

(defun output (string)
   (format t string)
   (force-output t))
(output "asdf")
(setq var (read))

And it still reads, then outputs. Am I not using force-output correctly or is this just an idiosyncrasy of SBCL?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use FINISH-OUTPUT.

In systems with buffered output streams, some output remains in the output buffer until the output buffer is full (then it will be automatically written to the destination) or the output buffer is explicity emptied.

Common Lisp has three functions for that:

  • FINISH-OUTPUT, attempts to ensure that all output is done and THEN returns.

  • FORCE-OUTPUT, starts the remaining output, but IMMEDIATELY returns and does NOT wait for all output being done.

  • CLEAR-OUTPUT, tries to delete any pending output.

Also the T in FORCE-OUTPUT and FORMAT are unfortunately not the same.

  • force-output / finish-output: T is *terminal-io* and NIL is *standard-output*

  • FORMAT: T is *standard-output*

this should work:

(format t "asdf")
(finish-output nil)   ;  note the NIL
(setq var (read))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...