# Constructor style pretty printing

**URL:** <https://racket.discourse.group/t/constructor-style-pretty-printing/2904>\
**Category:** Questions & Answers\
**Tags:** htdp\
**Created:** [May 5, 2024, 1:11am UTC](https://racket.discourse.group/t/constructor-style-pretty-printing/2904 "2024-05-05T01:11:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shhyou](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shhyou](https://racket.discourse.group/u/shhyou)\
**Post date:** [May 5, 2024, 1:11am UTC](https://racket.discourse.group/t/constructor-style-pretty-printing/2904/1 "2024-05-05T01:11:40Z")

</div>

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:

```racket
(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

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

```

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

---

<div class="post-metadata">

**Author:** ![robby](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/robby/32/7_2.png) [@robby](https://racket.discourse.group/u/robby)\
**Post date:** [May 5, 2024, 12:08pm UTC](https://racket.discourse.group/t/constructor-style-pretty-printing/2904/2 "2024-05-05T12:08:44Z")

</div>

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

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

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

```

produces this:

```scheme
(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))

```

---

<div class="post-metadata">

**Author:** ![shhyou](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shhyou](https://racket.discourse.group/u/shhyou)\
**Post date:** [May 7, 2024, 12:02am UTC](https://racket.discourse.group/t/constructor-style-pretty-printing/2904/3 "2024-05-07T00:02:15Z")

</div>

This works great, thank you!

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

> <https://github.com/racket/htdp/blob/8037f1cc620ed91fe68362ce5ad3d887269eb725/htdp-lib/htdp/bsl/runtime.rkt#L53-L66>

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

```racket
(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)))

```

---

<div class="post-metadata">

**Author:** ![robby](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/robby/32/7_2.png) [@robby](https://racket.discourse.group/u/robby)\
**Post date:** [May 7, 2024, 2:48pm UTC](https://racket.discourse.group/t/constructor-style-pretty-printing/2904/4 "2024-05-07T14:48:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shhyou](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shhyou](https://racket.discourse.group/u/shhyou)\
**Post date:** [May 9, 2024, 4:24pm UTC](https://racket.discourse.group/t/constructor-style-pretty-printing/2904/5 "2024-05-09T16:24:03Z")

</div>

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.
