Node:Pretty Printing, Next:, Previous:Value History, Up:Top

42 Pretty Printing

The module (ice-9 pretty-print) provides the procedure pretty-print, which provides nicely formatted output of Scheme objects. This is especially useful for deeply nested or complex data structures, such as lists and vectors.

The module is loaded by simply saying.

(use-modules (ice-9 pretty-print))

This makes the procedure pretty-print available. As an example how pretty-print will format the output, see the following:

(pretty-print '(define (foo) (lambda (x)
(cond ((zero? x) #t) ((negative? x) -x) (else (if (= x 1) 2 (* x x x)))))))
(define (foo)
  (lambda (x)
    (cond ((zero? x) #t)
          ((negative? x) -x)
          (else (if (= x 1) 2 (* x x x))))))

pretty-print obj [port] Scheme Procedure
Print the textual representation of the Scheme object obj to port. port defaults to the current output port, if not given.

Beware: Since pretty-print uses it's own write procedure, it's output will not be the same as for example the output of write. Consider the following example.

(write (lambda (x) x))
#<procedure #f (x)>

(pretty-print (lambda (x) x))

The reason is that pretty-print does not know as much about Guile's object types as the builtin procedures. This is particularly important for smobs, for which a write procedure can be defined and be used by write, but not by pretty-print.