printingcommon-lispsbclslime

How to print showing shared structure?


As an example, the two expressions evaluated below have the same "printed" representation, namely ((A) (A)), but they have very different internal structure. Here the structure is shared:

* (let ((item '(a))) (list item item))
((A) (A))

Here each (a) is a distinct entity:

* '((a) (a))
((A) (A))

Specifically, the value of the first expression is a two-element list were the two elements are identical; the value of the second expression is also a two-element list, but its two elements are distinct (i.e. not identical).

How can I represent these two values in a way that makes this difference in their internal structures more apparent?


Solution

  • What you are looking for is *print-circle*:

    * (setq *print-circle* t)
    T
    * '((a) (a))
    ((A) (A))
    * (let ((item '(a))) (list item item))
    (#1=(A) #1#)
    

    If you want "print - read consistency", you should investigate *print-readably* and with-standard-io-syntax:

    (equal (read-from-string (with-standard-io-syntax (write-to-string x)))
           x)
    

    should either return T or signal an error of type print-not-readable.