[plt-scheme] Re: pretty-printing

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sat Sep 2 16:44:11 EDT 2006

Paul Graham wrote:

 > Is there a way to pretty-print an expr in mzscheme?  There is
 > no reference to it in the index or toc of the manual...

Besides the builtin pretty print there is also pp-syntax, which
print expanded syntax-objects. This is handy during debugging
of macros.

<http://ja.soegaard.net/planet/html/soegaard/syntax.plt/current/doc.txt>


 > (require (planet "pp-syntax.ss" ("soegaard" "syntax.plt")))

 > (pp-syntax (expand
               '(begin
                  (letrec ((f (lambda (n)
                                (if (= n 0)
                                    1
                                    (* n (f (- n 1)))))))
                    (begin0 (f 5)
                            (set! x 1)))
                  (define y (let ((z 3)) 2)))))
[prints]

(begin
   (letrec ((f (lambda (n) (if (= n 0) 1 (* n (f (- n 1)))))))
     (begin0 (f 5) (set! x 1)))
   (define y (let ((z 3)) 2)))


For comparison

   (pretty-print (syntax-object->datum (expand <same expression>)))

prints

   (begin
     (letrec-values (((f)
                      (lambda (n)
                        (if (#%app = n (#%datum . 0))
                          (#%datum . 1)
                          (#%app
                           *
                           n
                           (#%app f (#%app - n (#%datum . 1))))))))
       (begin0 (#%app f (#%datum . 5)) (set! x (#%datum . 1))))
     (define-values
       (y)
       (let-values (((z) (#%datum . 3))) (#%datum . 2))))

-- 
Jens Axel Søgaard



Posted on the users mailing list.