Pretty-printing with format strings
pretty-fprintf
pretty-printf
pretty-eprintf
pretty-format
6.12

Pretty-printing with format strings

This module provides the pretty-printing versions of functions that format values based on a format string, like format or printf.

procedure

(pretty-fprintf out fmt v ...)  void?

  out : output-port?
  fmt : string?
  v : any/c
Prints the format string fmt to out, replacing each format-placeholder (such as ~v, ~s, or ~a) with a pretty-printing of the corresponding v.

Examples:
> (require pretty-format)
> (pretty-fprintf (current-output-port)
                  "~a: ~v"
                  "and the values are indented"
                  '(define (f n)
                     (for/list ([i (in-range n)] #:when (odd? i))
                       (* 2 i))))

and the values are indented: '(define (f n)

                                (for/list

                                 ((i (in-range n)) #:when (odd? i))

                                 (* 2 i)))

procedure

(pretty-printf fmt v ...)  void?

  fmt : string?
  v : any/c
Prints the format string fmt to the (current-output-port), replacing each format-placeholder (such as ~v or ~a) with a pretty-printing of the corresponding v.

Examples:
> (require racket/pretty
           pretty-format)
> (pretty-print-current-style-table
   (pretty-print-extend-style-table (pretty-print-current-style-table)
                                    '( Refine for/and for/and*)
                                    '(lambda lambda lambda lambda)))
> (pretty-printf "val ~a : ~s"
                 'rem-dups
                 '( (X)
                     (-> ([xs : (Listof X)])
                         ([f : (-> X X Bool)])
                         (Refine
                          [ys : (Listof X)]
                          (subset? ys xs)
                          (for/and ([x (in-list xs)])
                            (member x ys f))
                          (for*/and ([i (in-range (length ys))]
                                     [j (in-range i)])
                            (not (f (nth ys i) (nth ys j))))))))

val rem-dups : (∀ (X)

                 (->

                  ((xs : (Listof X)))

                  ((f : (-> X X Bool)))

                  (Refine (ys : (Listof X))

                    (subset? ys xs)

                    (for/and ((x (in-list xs))) (member x ys f))

                    (for*/and

                     ((i (in-range (length ys))) (j (in-range i)))

                     (not (f (nth ys i) (nth ys j)))))))

procedure

(pretty-eprintf fmt v ...)  void?

  fmt : string?
  v : any/c
Prints the format string fmt to the (current-error-port), replacing each format-placeholder (such as ~v or ~a) with a pretty-printing of the corresponding v.

procedure

(pretty-format fmt v ...)  string?

  fmt : string?
  v : any/c
Returns the string produced from replacing each format-placeholder in fmt with the pretty-printed version of the corresponding v.

Examples:
> (require racket/pretty
           pretty-format)
> (pretty-print-columns 60)
> (define str
    (pretty-format "val ~a : ~v = ~s"
                   'f
                   procedure?
                   '(λ (n)
                      (if (zero? n)
                          1
                          (* n (f (sub1 n)))))))
> (string-length str)

189

> (displayln str)

val f : #<procedure:procedure?> = (λ (n)

                                    (if (zero? n)

                                      1

                                      (* n (f (sub1 n)))))