SBCL printing output twice
While learning the basics of the SBCL REPL you may have noticed its strange behaviour when it comes to the output of some instructions.
Making a sum will produce a single output with the result of the operation.
* (+ 1 5) 6
The same will happen when calculating the square root of a number.
* (sqrt 4) 2.0
But if we try to print a string using the
* (print "twice") "twice" "twice"
In order to understand why a blank line is added to the output, we can try using another printing function called
Its output is equal to
* (prin1 "twice") "twice" "twice"
As you can see from the functions’ documentation, the only difference between them is exactly this empy line (a new line actually) printed before the output.
But we have not yet discovered why the string is printed twice.
The mystery is quite simple to solve, because the first string printed by the function is its output in the standard out.
The second string is the value returned by the function and printed by the REPL - in a non-interactive program you will never see this behaviour.
If you need another prove of this behaviour you could try to assign the output of the
str) and then transforming it in uppercase.
* (let ((str (print "Hello, world!"))) (string-upcase str)) "Hello, world!" "HELLO, WORLD!"
Here the behaviour is clear.
The blank line is added by