The less-familiar parts of Lisp for beginners — pprint-fill, pprint-linear, pprint-tabular

We’ve been looking over the pretty-printing functions recently.  These functions, pprint-fill, pprint-linear, and pprint-tabular, are used in the pretty-printing of lists.  Their special behaviour is only manifested when the *print-pretty* variable is non-nil.

The pprint-fill function prints as many list elements on a line as possible before inserting a newline.  Naturally, this raises the question of what the Lisp image thinks is the line length.  The standard defines a variable, *print-right-margin*, which, if set, is taken to be the maximum line length.  If this variable is nil, then the maximum line length is determined in an implementation-defined manner.  If the current cursor position is not at the left margin, the printing is indented appropriately on the left, so that the screen region being considered for printing is narrowed appropriately.

The pprint-linear function will either print all of the list elements on a single line, or if that would exceed the length of the line, prints exactly one element per line.  The behaviour when the cursor is not in the left column is the same as the case above.

The pprint-tabular function prints elements in a tabular fashion, with consecutive elements being printed left-to-right.  The tabsize optional parameter determines the number of spaces from the beginning of one column to the beginning of the next.  The behaviour when the cursor is not in the left column is the same as the case above.

Here are some examples to demonstrate the behaviour of these functions:
*slime-repl sbcl*

CL-USER> (setq *print-pretty* t)
T
CL-USER> (setq *print-right-margin* 50)
50
CL-USER> (let (demolist)
           (dotimes (i 6)
             (push (* i 10000) demolist))
           (princ "Demolist is: ")
           (pprint-linear t demolist))
Demolist is: (50000 40000 30000 20000 10000 0)
NIL
CL-USER> (let (demolist)
           (dotimes (i 7)
             (push (* i 10000) demolist))
           (princ "Demolist is: ")
           (pprint-linear t demolist))
Demolist is: (60000
              50000
              40000
              30000
              20000
              10000
              0)
NIL
CL-USER> (let (demolist)
           (dotimes (i 7)
             (push (* i 10000) demolist))
           (princ "Demolist is: ")
           (pprint-fill t demolist))
Demolist is: (60000 50000 40000 30000 20000 10000
              0)
NIL
CL-USER> (let (demolist)
           (dotimes (i 7)
             (push (* i 10000) demolist))
           (princ "Demolist is: ")
           (pprint-tabular t demolist t nil 10))
Demolist is: (60000     50000     40000     30000
              20000     10000     0)
NIL

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

反垃圾邮件 / Anti-spam question * Time limit is exhausted. Please reload CAPTCHA.