Christophe Grand a écrit :
> Timothy Pratley a écrit :
>   
>> I want to grow a tree programmatically so have been trying zippers:
>>
>> (defn insert-parent [loc n]
>>   (clojure.zip/replace loc (clojure.zip/make-node
>>                              loc n loc)))
>> (println (clojure.zip/root (insert-parent (clojure.zip/seq-zip (list
>> 1)) 2)))
>> (println (clojure.zip/root (clojure.zip/append-child
>>                              (clojure.zip/seq-zip (list 1)) 2)))
>>
>> [(1) nil]
>> (1 2)
>>
>> My 'insert-parent' function does not work, I was trying to get (2 1) -
>> any tips?
>>   
>>     
>
> It's clojure.zip/insert-child you are looking for:
>
> clojure.contrib.struct-ed=> (clojure.zip/root (clojure.zip/insert-child 
> (clojure.zip/seq-zip (list 1)) 2))
> (2 1)
>
> Christophe
>   

My take on your followup question (asked on #clojure):

If you want to add a parent you have to know how to make a new node with 
a single child. Since you are using seq-zip, it's easy: you simply use 
'list ((list x) yields indeed a node with one child: x)!
user=> (-> '(1) clojure.zip/seq-zip (clojure.zip/edit list) 
clojure.zip/root)
((1))

Then you can easily add 2:
user=> (-> '(1) clojure.zip/seq-zip (clojure.zip/edit list) 
(clojure.zip/insert-child 2) clojure.zip/root)
(2 (1))

or in a single step:
user=> (-> '(1) clojure.zip/seq-zip (clojure.zip/edit #(list 2 %)) 
clojure.zip/root)
(2 (1))


Christophe

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to