Pau*_*des 1 sbcl common-lisp data-structures clozure-cl
在玩图表时,我得到了一个奇怪的错误,我不太明白.下面的代码重现了这个问题.
;; Define struct to store a node with links to other nodes.
(defstruct node properties links)
;; Make two nodes
(setf a (make-node :properties '(:name a))
b (make-node :properties '(:name b)))
;; Create link from b to a. This works fine...
(push b (node-links a))
;; ... but this crosslink makes lisp chase its own tail for a while and then crash with a stack overflow.
(push a (node-links b))
Run Code Online (Sandbox Code Playgroud)
我和SBCL和Clozure得到了相同的结果.设置*print-length*
为可管理的值不起作用.
所以我的问题是:为什么这段代码不能创建与循环列表相同类型的无限打印循环(即没有堆栈溢出和Ctrl-C可停止).任何输入都表示赞赏.
谢谢,保罗
*print-length*
控制列表中的元素数量.你在找*print-level*
.这对我来说很好.
(let ((*print-level* 3))
(format t "~W~%" a))
;; Output: #S(NODE :PROPERTIES (:NAME A)
;; :LINKS (#S(NODE :PROPERTIES # :LINKS #)))
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用它*print-circle*
来检测周期并以更好的方式打印它们.
(let ((*print-circle* t))
(format t "~W~%" a))
;; Output: #1=#S(NODE :PROPERTIES (:NAME A)
;; :LINKS (#S(NODE :PROPERTIES (:NAME B) :LINKS (#1#))))
Run Code Online (Sandbox Code Playgroud)
在这里,它实际上检测循环和打印#1#
,#1=
对它的引用表明它是同一个对象.