www.delorie.com/gnu/docs/guile/guile_501.html   search  
 
Buy GNU books!


Guile Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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

Scheme Procedure: pretty-print obj [port]
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))
-|
#[procedure]

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.


  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003