Clojure - 重置!原子导致堆栈溢出
我正在学习 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 是有效的,但将其重置为根本原因堆栈溢出。你能帮我理解为什么吗?
I am learning clojure and just playing with atoms and refs. Here is a simple tree implementation, but it is causing a stack overflow when executing the add-child method:
(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 ())))
Here is a sample session from 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.
As you can see above, reseting parent of child to child2 works, but resetting it to root causes a stack overflow. Can you help me understand why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它实际上不是
reset!
而是 REPL 尝试打印(无限)嵌套值。试试这个:
这将限制分层对象的递归打印。
It's not actually the
reset!
but the REPL trying to print the (infinitely) nested values.Try this:
That will limit the recursive printing of hierarchical objects.