我有一个非常简单的数据结构,我在Lisp中定义:
;;Data structure for a person
(defstruct person
(name nil)
(age 0)
(siblings nil :type list)) ;; Siblings is a list of person objects
Run Code Online (Sandbox Code Playgroud)
然后我继续实例化一些人物:
(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))
Run Code Online (Sandbox Code Playgroud)
然后,我将兄弟姐妹联系起来(假设他们都是彼此的兄弟姐妹):
(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))
Run Code Online (Sandbox Code Playgroud)
然后,我如何打印有关我已实例化和修改的对象的信息?我已经研究了关于print-object和print-function的defstruct选项,但我无法弄清楚如何正确打印我的对象.使用类似的东西:
(print person-a)
Run Code Online (Sandbox Code Playgroud)
将我的ACL解释器发送到无限循环.
常见的lisp有一个控制递归结构打印的变量:*print-circle*.在你的Lisp中nil,默认情况下可能是false()(因为它在SBCL和clisp中 - 我不熟悉ACL),这可能导致无限循环.如果将其设置为t,则应打印结构:
(setf *print-circle* t)
Run Code Online (Sandbox Code Playgroud)
我用以下文件测试了这个:
(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)
Run Code Online (Sandbox Code Playgroud)
以下是运行该代码的脚本:
> 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)
Run Code Online (Sandbox Code Playgroud)
如果我离开*print-circle* nil,那么我得到一个堆栈溢出错误(SB-KERNEL::CONTROL-STACK-EXHAUSTED在sbcl中).
HTH,
凯尔