在Lisp中打印defstruct [英] Print defstruct in Lisp
问题描述
我在Lisp中定义了一个非常简单的数据结构:
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))
然后如何打印有关实例化和修改的对象的信息?我已经研究了有关打印对象和打印功能的defstruct选项,但无法弄清楚如何正确打印对象.使用类似的东西:
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)
将我的ACL解释器发送到无限循环中.
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 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)
如果我离开*print-circle*
nil
,那么我会得到一个堆栈溢出错误(sbcl中的SB-KERNEL::CONTROL-STACK-EXHAUSTED
).
If I leave *print-circle*
nil
, then I get a stack overflow error (SB-KERNEL::CONTROL-STACK-EXHAUSTED
in sbcl).
HTH,
凯尔
这篇关于在Lisp中打印defstruct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!