Next: iklib printing debug, Previous: iklib printing formatting, Up: iklib printing [Index]
Printi Scheme data, typically Scheme programs, in a format close to how
a Scheme programmer would write it. Unlike write
, which writes
its input all in one line, pretty-print
inserts spaces and new
lines in order to produce more pleasant output.
(define fact-code '(letrec ([fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))]) (fact 5))) > (pretty-print fact-code) (letrec ((fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))))) (fact 5))
The second argument to pretty-print
, if supplied, must be an open
textual output port. If not supplied, the current-output-port
is
used.
Like pretty-print
but accepts the additional arguments:
A non–negative fixnum. The zero–based column offset at which the first line is displayed. When printing a multiline symbolic expression, the column offset of the first line depends upon what has been already printed on such line; this argument allows us to correctly align the second and subsequent lines. For example the following program:
#!r6rs (import (vicare)) (pretty-width 10) (set-port-buffer-mode! (current-output-port) (buffer-mode none)) (display "01234") (pretty-print '("56789" "8 7 6 5")) (newline) (display "01234") (pretty-print* '("56789" "8 7 6 5") (current-output-port) 5 #t)
prints:
01234("56789" "8 7 6 5") 01234("56789" "8 7 6 5")
Interpreted as boolean value. If true: a newline character is output after the symbolic expression.
The parameter pretty-width
controls the number of characters
after which the pretty-print
starts breaking long lines into
multiple lines. The initial value of pretty-width
is set to 60
characters, which is suitable for most terminals and printed material.
> (parameterize ([pretty-width 40]) (pretty-print fact-code)) (letrec ((fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1))))))) (fact 5))
Note that pretty-width
does not guarantee that the output will
not extend beyond the specified number. Very long symbols, for
examples, cannot be split into multiple lines and may force the printer
to go beyond the value of pretty-width
.
Still undocumented.
If set to true: print Scheme Unicode characters outside of the printable
ASCII range in hex format, else print them using the encoding of the
output port. This parameter is initialised to #f
.
The radix used to print exact integers (fixnums and bignums) with
write
, display
and similar functions. Initialised to
10
, it must hold a value usable as second argument to
string->number
.
Control how gensyms are printed by the various writers.
#f
: gensym syntax is suppressed by the writers and
only the gensyms’ pretty names are printed.
#t
: the full #{pretty unique}
syntax is
printed.
pretty
: then gensyms are printed using
the #:pretty
notation.
This parameter is initialised to #t
. Examples:
> (parametrise ((print-gensym #f)) (pretty-print (list (gensym) (gensym)))) (g0 g1) > (parametrise ((print-gensym #t)) (pretty-print (list (gensym) (gensym)))) (#{g2 |KR1M2&CTt1<B0n/m|} #{g3 |FBAb&7NC6&=c82!O|}) > (parametrise ((print-gensym 'pretty)) (pretty-print (list (gensym) (gensym)))) (#:g4 #:g5)
Specify the string to be used as the prefix to generated pretty names. The initialisation value is the string ‘g’, which causes generated strings to have pretty names in the sequence ‘g0’, ‘g1’, ‘g2’, etc.
> (parametrise ((gensym-prefix "var") (print-gensym #f)) (pretty-print (list (gensym) (gensym) (gensym)))) (var0 var1 var2)
This parameter controls how pretty names are generated, and has nothing
to do with how gensym
constructs a new gensym; in particular,
knowing that it is pretty-print
that generates the pretty names,
notice the difference between the output in the first example with the
output of the examples below:
> (pretty-print (parametrise ((gensym-prefix "var") (print-gensym #f)) (list (gensym) (gensym) (gensym)))) (g3 g4 g5) > (let ((ls (list (gensym) (gensym) (gensym)))) (parametrise ((gensym-prefix "var") (print-gensym #f)) (pretty-print ls))) (var5 var6 var7)
Determine the number which is attached to the gensym-prefix
when
gensyms’ pretty names are generated. The initialisation value is
‘0’ and it is incremented every time a pretty name is generated.
It might be set to any non–negative integer value.
> (let ((x (gensym))) (parametrise ((gensym-count 100) (print-gensym #f)) (pretty-print (list (gensym) x (gensym))))) (g100 g101 g102)
Notice from all the examples so far that pretty names are generated in the order at which the gensyms are printed, not in the order in which gensyms were created.
Next: iklib printing debug, Previous: iklib printing formatting, Up: iklib printing [Index]