I do not understand about structural separation in Clojure. The following is the xconj function, taken from Joy Clojure (The Great Book of BTW).
;;Building a naive binary search tree using recursion
(defn xconj [t v]
(cond
(nil? t) {:val v :L nil :R nil}
(< v (:val t)) {:val (:val t) :L (xconj (:L t) v) :R (:R t)}
:else {:val (:val t) :L (:L t) :R (xconj (:R t) v)}))
If you define two trees t1 and t2, as shown below.
(def t1 (xconj (xconj (xconj nil 5) 3) 2))
(def t2 (xconj t1 7))
Clearly, the left subtree is divisible by t1 and t2
user> (identical? (:L t1) (:L t2))
true
But if you had to create a new tree t3 by adding a new value of '1' to the left subtree of t1, like this:
(def t3 (xconj t1 1))
Will it be the result of a completely new tree with all the copied values and without structural exchange, or will any structure still be split? What if the left branch is larger, then 2> 3-> 4-> 5-> 6-> 7 (* root) and 1 was inserted into the left subtree, then some division of the structure will be preserved?
source
share