I have a loop:
(defun echo-lines ()
(loop while
(let ((line (read-line)))
(if (equal line "done")
nil
(print line)))))
I expect it to echo the user’s input after they end the line, unless they have typed “done”, in which case it stops. Instead, it echos a blank line the first time, after that, it echos the input from one previous.
Example:
* (echo-lines)
Hello, loop.
This is my second line.
"Hello, loop."
This is my third.
"This is my second line."
I'm almost done.
"This is my third."
done
"I'm almost done."
NIL
Expected:
* (echo lines)
Hello, loop.
"Hello, loop."
done
NIL
The effect is caused by a mix of buffered output and the definition of
PRINT.PRINTdoes not cause output to be written immediately if the underlying output stream is buffered. Note also thatPRINTprints the newline first. Note that for portable code you should assume that an output stream is buffered. This means that you need to make sure that buffered output is written when you need it. UseFORCE-OUTPUT(doesn’t wait) andFINISH-OUTPUT(waits).This is a slightly rewritten version.