2022-07-04: Basic Show Procedure

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.

Basic Show

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:

Output to File or Port

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 ...


Page from Peter's Scrapbook, output from a VimWiki on 2024-01-29.