Constructor style pretty printing

In HtDP, the structs are printed using the (make-XXX ...) constructor style either by default in REPL or via print. How do I get the same behavior for pretty printing?

I have been able to print values in constructor style via the pconvert library:

(require (only-in lang/htdp-intermediate-lambda
                  define-struct
                  make-posn)
         mzlib/pconvert)

(install-converting-printer)
(constructor-style-printing #t)

(define-struct game [snake foods obstacles ticks])

(define g
  (make-game "snake" (list) (list (make-posn 3 4)) 0))

g

However, the pconvert printer does not apply to pretty-print. The best I can get is

(parameterize ([print-as-expression #t])
  (pretty-print g))

but this one does not use the make-game and make-posn constructors.

I think the idea is that you use pconvert directly and then pretty-write the result. Something like this (building on your code above):

(define (pp x)
  (pretty-write
   (print-convert x #t)))

(parameterize ([pretty-print-columns 80])
  (pp (for/list ([i (in-range 20)]) g)))

produces this:

(list
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0)
 (make-game "snake" empty (list (posn 3 4)) 0))

This works great, thank you!

Starting with print-convert, I found the desired configuration:

In particular, I need to set at least these three parameters:

(parameterize ([constructor-style-printing #t]
               [add-make-prefix-to-constructor #t]
               [abbreviate-cons-as-list #t]
               [print-as-expression #f])
  (pretty-write (print-convert g)))

Great! I'm glad you got something sorted out.

Is this for use with the teaching languages? If it is for #lang-based language, there may be a way to get the language to cough up its printer and that might end up being more modular.

It's for use in the script. The teaching languages don't pretty print the values, however, so the outputs are often difficult to read.

1 Like