我正在学习clojure,只是玩原子和参考。这是一个简单的树实现,但它在执行add-child方法时导致堆栈溢出:
(ns mytree)
(defprotocol PNode
(add-child [this parent] "add 'this' node to the parent"))
(defrecord Node [id parent name children]
PNode
(add-child [this p]
(println "adding this to children of parent")
(dosync
(alter (:children p) conj this))
;following causes a stack overflow -why?
; (println "reseting parent of this")
; (reset! parent p)
))
(defn create-node [id name]
(Node. id (atom nil) name (ref ())))
以下是REPL的示例会话:
>mytree> (def root (create-node 1 "CEO"))
>#'mytree/root
>mytree> (def child (create-node 2 "VP1"))
>#'mytree/child
>mytree> (add-child child root)
>adding this to children of parent
>(#:mytree.Node{:id 2, :parent #<Atom@4dffa9d: nil>, :name "VP1", :children #<Ref@cbe5beb: ()>})
>mytree> root
>#:mytree.Node{:id 1, :parent #<Atom@44896098: nil>, :name "CEO", :children #<Ref@2a75733a: >(#:mytree.Node{:id 2, :parent #<Atom@4dffa9d: nil>, :name "VP1", :children #<Ref@cbe5beb: ()>})>}
>mytree> (def child2 (create-node 3 "VP2"))
>#'mytree/child2
resetting parent to child2 works
>mytree> (reset! (:parent child) child2)
>#:mytree.Node{:id 3, :parent #<Atom@43c32b82: nil>, :name "VP2", :children #<Ref@425d868f: ()>}
but resetting parent to root causes stack overflow - why??
>mytree> (reset! (:parent child) root)
>; Evaluation aborted.
如上所示,将child的parent重置为child2可正常工作,但将其重置为root会导致堆栈溢出。你能帮我理解为什么吗?
答案 0 :(得分:12)
实际上并不是reset!
,而是REPL试图打印(无限)嵌套值。
试试这个:
(set! *print-level* 10)
这将限制分层对象的递归打印。