Re: printing circular data structures



On 2007-11-30, Eli Bendersky <eliben@xxxxxxxxx> wrote:
Hello,

I want to be able to print out data structures, without having the
environment blow up if the data structure is circular. One way to do
this is to limit the amount of characters the printer prints. Is this
possible in Common Lisp ?

The implementation I'm using is CLISP, though I suspect there can be a
standard way, since I want it to work for all my printing functions
(print, format, etc.)

Hi Eli,
you should look at the *print-<..>* variables:
http://www.lispworks.com/documentation/HyperSpec/Front/X_Perm_P.htm

*print-cirle* and *print-level* might be of interest for example.

You can also provide a more specific print-object method? E.g.:

(defclass my-class ()
((my-slot :accessor my-class-my-slot
:initform "the value")))

(defmethod print-object ((mc my-class) (s stream))
(format s "#MY-CLASS WITH MY-SLOT=~A~%" (my-class-my-slot mc)))

and then you will get:

CL-USER> (make-instance 'my-class)
#MY-CLASS WITH MY-SLOT=the value

CL-USER>

.... so you can basically do exactly whatever you want with the object.

--
Oyvin
.



Relevant Pages

  • Re: printing circular data structures
    ... I want to be able to print out data structures, ... environment blow up if the data structure is circular. ... I wish it was possible to retract Google Groups messages withing some ...
    (comp.lang.lisp)
  • Re: Armstrong and Virdings One Pass GC
    ... >>Languages with non-mutable data structures will never get circular ... > data structures *with strict semantics*, as any circular definition ... of cycles is statically known (you can't write a function which makes ...
    (comp.lang.functional)
  • Re: Armstrong and Virdings One Pass GC
    ... Robert Virding wrote: ... >Well we have no circular references in Erlang and still manage to do ... data structures *with strict semantics*, ...
    (comp.lang.functional)