Search code examples
formatcommon-lisppretty-print

Printing a list in outline form


Can the common-lisp pretty printer be easily configured to print out any deeply nested list in "outline" form, or is this a job for format? Eg, '(a (b c (d e (f)) g)) should come out looking something like the following, where each cdr element steps down a level from the car:

A
 B
 C
  D
  E
   F
 G

Solution

  • Look at the ~nT format directive. This will print the next argument at the n-th column:

    (format t "~30T~a" 'a)
                                  A
    

    If the column is variable, then use ~vt to use the first argument as the column value:

    (format t "~VT~a" 10 'a)
              A
    

    This will print 'A' at the 10-th column