|printing recursive objects? email@example.com (Paul Haahr) (1993-05-03)|
|Re: printing recursive objects? firstname.lastname@example.org (John Lacey) (1993-05-04)|
|printing recursive objects? email@example.com (Tony Kanawati) (1993-05-04)|
|printing recursive objects? firstname.lastname@example.org (1993-05-05)|
|From:||"John Lacey" <email@example.com>|
|Organization:||Computer Science, Indiana University|
|Date:||Tue, 4 May 1993 06:46:40 GMT|
Paul Haahr <firstname.lastname@example.org> writes:
> For example, given the scheme expression
> (let ((x (cons 'a nil))) (set-cdr! x x) x)
> how do I print some scheme expression that, when interpreted,
> creates the same structure, rather than just a lot of ``a''s?
Chez Scheme has a parameter called print-graph. When enabled, write prints
objects in a notation which may be read in later. (Of course, this
requires the cooperation of read, as well.)
The notation is that the first occurence of a duplicated object is written
as `#n=object', and subsequent occurences of the object are written as
`#n#'. Your example would be printed as `#0=(a . #0#)'.
Of course a simple table lookup will suffice to detect duplicated objects.
I don't, unfortunately, have any references, or knowledge of more
complicated algorithms, if such exist. (The description in the Chez System
Manual is no more detailed than that the above.)
Return to the
Search the comp.compilers archives again.