I have a very simple data structure that I have defined in Lisp:
;;Data structure for a person
(defstruct person
(name nil)
(age 0)
(siblings nil :type list)) ;; Siblings is a list of person objects
I then procede to instantiate a few person objects:
(setf person-a (make-person :name 'Tim :age 23))
(setf person-b (make-person :name 'Sally :age 21))
(setf person-c (make-person :name 'Louis :age 24))
I then relate the siblings (assume they are all siblings of each other):
(setf (person-siblings person-a) (list person-b person-c))
(setf (person-siblings person-b) (list person-a person-c))
(setf (person-siblings person-c) (list person-b person-a))
How can I then print information about the objects that i've instantiated and modified? I have looked into the options of a defstruct with regard to print-object and print-function but I cannot figure out how to properly print my objects. Using something like:
(print person-a)
sends my ACL interpreter into an infinite loop.
Common lisp has a variable that controls the printing of recursive structures:
*print-circle*
. In your Lisp it may be false (nil
) by default (as it is in SBCL and clisp - I'm not familiar with ACL), which is probably causing the infinite loop. If you set it tot
, your structures should print:I tested this with the following file:
Here is a transcript from running that code:
If I leave
*print-circle*
nil
, then I get a stack overflow error (SB-KERNEL::CONTROL-STACK-EXHAUSTED
in sbcl).HTH,
Kyle