How to format a tree so that it works with Clojure zipper?

I create s-expression trees for the genetic programming problem and need to change parts of the trees during evolution. I stumbled upon a Clojure zipper , which seems like it should be perfect, but for my life I can't figure out how to use it.

For example, let's say I create a zipper with

(def zipped (zip/seq-zip `(+ (- 1 2) 3)))

I understand this to represent a tree with + in the root, which looks like this:

   +
 -   3
1 2

My lightning, however, does not agree with this: if I ask the first node with (-> zipped zip/down zip/node), it will give me +(that's right), but it (-> zipped zip/down zip/down)does not accept me -, instead it returns nil. In fact, (-> zipped zip/down zip/rights)gives the rest of the tree like brothers and sisters to the right of the root, which tells me that I have no tree at all:

user> (-> zipped zip/down zip/rights)
((clojure.core/- 1 2) 3)

I am sure that I correctly represent my trees, because when I execute them, I get the correct answer. Is the zipper awaiting another layout?

+3
source share
2 answers

, . - , , LISP :

'(+ (- 1 2) 3) ( + ( - 1 2) 3), :

+  .     3
   - 1 2

(-> zipped down node) +, . (-> zipped down down), , , + . (-> zipped down right down node) , , .

+3

- , . node 3 : +, (- 1 2) 3. down node, , +.

-, :

user=> (-> zipped zip/down zip/right zip/down zip/node)
clojure.core/-
+1

Source: https://habr.com/ru/post/1768917/


All Articles