In the spirit of (srfi 28), I
define a version of show
which is to (scheme show)
/ (srfi 159)
as
(srfi 28)
is to slib's implementation of Common Lisp's format
.
The idea is not complicated:
(define (show . args) (for-each display args))
And it is easy to use:
gosh$ (show "Hello" #\newline) Hello gosh$ (show "Hello " #\newline "... world" #\newline) Hello ... world gosh$ (define x '(1 2 3)) x gosh$ (show "X is " x #\newline) X is (1 2 3) gosh$ (show "X is " x " and has " (length x) " elements" #\newline) X is (1 2 3) and has 3 elements
The equivalent in (srfi 28)
to the last statement above is:
gosh$ (display (format "X is ~a and has ~a elements~%" x (length x))) X is (1 2 3) and has 3 elements
Using (scheme show)
:
gosh$ (show #t "X is " x " and has " (length x) " elements" nl) X is (1 2 3) and has 3 elements
And finally (slib format)
:
gosh$ (format #t "X is ~a and has ~a elements~%" x (length x)) X is (1 2 3) and has 3 elements
Notice that:
-
There is no control of the output destination: SRFI 28's
format
produces strings, and the remaining two take an optional port or#t
argument. Basicshow
always uses thecurrent-output-port
. -
#\newline
is used to start a new line, rather than some special syntax ("~%") or variable (nl
). -
Unlike srfi 28, no distinction is made between
display
andwrite
: I only usedisplay
. As the arguments are processed as a batch, this is hard to control on an individual level.
Basic show
outputs to current-output-port
, so we can get output to a file
by using with-output-to-file
:
gosh$ (with-output-to-file "example.txt" (lambda () (show "X is " x " and has " (length x) " elements" #\newline))) gosh$ > more .\example.txt X is (1 2 3) and has 3 elements
This works with R5RS Schemes, such as Scheme 48.
With R7RS, current-output-port
is a parameter, and so we can redefine it to
any port, such as a string output port:
gosh$ (parameterize ((current-output-port (open-output-string))) ......(show "Output to string") ......(get-output-string (current-output-port))) "Output to string"
If necessary, show
could be redefined to check if the first argument is a
port, and then display to that port ...