Print defstruct in Lisp

寵の児 提交于 2019-12-01 21:50:38
Kyle Burton

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!