0
#!/usr/bin/sbcl --script

(defvar a)
(defun main ()
  ;(write-line "Yes or no?"); This works in the correct order
  (print "Yes or no?")
  (setq a (read))
  (print a))
(main)

When executed it prompts for input and only after giving the input it prints "Yes or no?" (where my intention is for it to first print "Yes or no?" then take input). When I swap print for write-line the order is as expected, though. Why does write-line behave so differently from print in this case? Why does it seem that the order of execution is not top->down in this function? I want the function main execute print and read in the order that they appear in the source file. How might I accomplish this?

John Smith
  • 835
  • 1
  • 7
  • 19
  • @MartinPůda Thanks, it mostly does. I added a note about different behaviour of `write-line` to my question. It may not merit the answer but a comment would be appreciated. – John Smith Jul 05 '22 at 07:20
  • 2
    This behavior isn't exclusive to Common Lisp; the underlying operating system buffers i/o, and that buffer must be flushed to send output to the desired device. "_Why does `write-line` behave so differently from `print` in this case?_" Typically a newline will flush the output buffer, and `write-line` adds a newline to the characters it sends to output, but `print` does not add that newline. Note that it is often best to explicitly flush the buffer, e.g., using `finish-output` or `force-output` instead of relying on newline behavior. – ad absurdum Jul 05 '22 at 14:54
  • 1
    typically newlines flush in terminals, but not in file streams – coredump Jul 06 '22 at 18:57

0 Answers0