Print defstruct in Lisp

2019-02-24 10:37发布

问题:

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.

回答1:

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 to t, your structures should print:

(setf *print-circle* t)

I tested this with the following file:

(defstruct person
  (name nil)
  (age 0)
  (siblings nil :type list))

(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))

(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-a person-b))

(setf *print-circle* t)
(format t "~a~&" person-a)
(format t "~a~&" person-b)
(format t "~a~&" person-c)

(print person-a)
(print person-b)
(print person-c)

Here is a transcript from running that code:

> sbcl
This is SBCL 1.0.55, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* (load "defstruct.lisp")

#1=#S(PERSON
        :NAME TIM
        :AGE 23
        :SIBLINGS (#2=#S(PERSON
                         :NAME SALLY
                         :AGE 21
                         :SIBLINGS (#1#
                                    #3=#S(PERSON
                                          :NAME LOUIS
                                          :AGE 24
                                          :SIBLINGS (#1# #2#))))
                   #3#))
#1=#S(PERSON
      :NAME SALLY
      :AGE 21
      :SIBLINGS (#2=#S(PERSON
                       :NAME TIM
                       :AGE 23
                       :SIBLINGS (#1#
                                  #3=#S(PERSON
                                        :NAME LOUIS
                                        :AGE 24
                                        :SIBLINGS (#2# #1#))))
                 #3#))
#1=#S(PERSON
      :NAME LOUIS
      :AGE 24
      :SIBLINGS (#2=#S(PERSON
                       :NAME TIM
                       :AGE 23
                       :SIBLINGS (#3=#S(PERSON
                                        :NAME SALLY
                                        :AGE 21
                                        :SIBLINGS (#2# #1#))
                                  #1#))
                 #3#))

#1=#S(PERSON
      :NAME TIM
      :AGE 23
      :SIBLINGS (#2=#S(PERSON
                       :NAME SALLY
                       :AGE 21
                       :SIBLINGS (#1#
                                  #3=#S(PERSON
                                        :NAME LOUIS
                                        :AGE 24
                                        :SIBLINGS (#1# #2#))))
                 #3#)) 
#1=#S(PERSON
      :NAME SALLY
      :AGE 21
      :SIBLINGS (#2=#S(PERSON
                       :NAME TIM
                       :AGE 23
                       :SIBLINGS (#1#
                                  #3=#S(PERSON
                                        :NAME LOUIS
                                        :AGE 24
                                        :SIBLINGS (#2# #1#))))
                 #3#)) 
#1=#S(PERSON
      :NAME LOUIS
      :AGE 24
      :SIBLINGS (#2=#S(PERSON
                       :NAME TIM
                       :AGE 23
                       :SIBLINGS (#3=#S(PERSON
                                        :NAME SALLY
                                        :AGE 21
                                        :SIBLINGS (#2# #1#))
                                  #1#))
                 #3#)) 
T
* (sb-ext:quit)

If I leave *print-circle* nil, then I get a stack overflow error (SB-KERNEL::CONTROL-STACK-EXHAUSTED in sbcl).

HTH,

Kyle