What Mike said. It's basically the same problem as for serialization, so you keep a hashtable of all objects traversed---but you need not record objects that have no subobjects and have "simple" or "short" printed representations (such as numbers and maybe short strings).
In full generality it requires two traversals. Then as you format each object on the second traversal, objects encountered more than once can be given labels on first occurrence and references on later occurrences. Common Lisp uses "#nnn=" for labels and "#nnn# for references, for integers nnn, so a list containing three occurrences of the same sublist as well as two self-references and the symbol BAZ might print in this way: #1=(#1# #2=(a b c) #2# #1# BAZ #2#) Common Lisp has a global (dynamically scoped) variable *print-circle* that controls whether to use the circularity-detection algorithm. --Guy On Aug 28, 2013, at 6:54 PM, Alan Eliasen <elia...@mindspring.com> wrote: > On 08/28/2013 04:47 PM, Guy Steele wrote: >> <oldfogey> *ahem* Y'know, Common Lisp had a good solution for >> printing self-referential structures (by a user-extensible print >> function) back in 1984. >> >> It leaned on the solution that had been provided in Interlisp in >> 1974. On a machine with one megabyte of main memory and a speed of 1 >> MIPS. >> >> This is not rocket science. </oldfogey> > > Hehe. So could you please describe the solution? > > -- > Alan Eliasen > elia...@mindspring.com > http://futureboy.us/