Dave,
Your first attempt looks OK to me.
(require '(clojure [zip :as z]))
(def zipper (z/vector-zip [:A [:B [:D :E]] [:C [:F :G]]]))
(defn pre-order [loc]
(when-not (z/end? loc)
(when-not (z/branch? loc)
(println (z/node loc)))
(recur (z/next loc))))
user=> (pre-order zipper)
:A
:B
:D
:E
:C
:F
:G
nil
user=>
What made you think it didn't work?
-mark
On Monday, October 15, 2012 9:20:14 PM UTC+1, Dave Kincaid wrote:
>
> I stumbled across the clojure.zip library today which looks extremely
> useful for working with trees. The only problem is I can't figure out how
> to represent a tree as a vector so that the zipper will have the correct
> structure. For example say I want to work with this tree:
>
>
> <https://lh5.googleusercontent.com/-mq1roiqfqw4/UHxvMDQUIfI/AAAAAAAAA_o/mNA9pAkOEIs/s1600/tree.png>
> my first intuition was a vector like this [:A [:B [:D :E]] [:C [:F :G]]] but
> that didn't work. Then I thought of something like this: [[:B [:D :E]] :A
> [:C [:F :G]]]. It seems like that gets me a little bit closer since now
> the zipper at least gets that :A has two children.
>
> Can anyone help me understand how to represent a tree in a Clojure vector?
> Is there a common lispy way of doing this that everyone already knows? It
> seems like all of the references I could find online all assume that you
> know how to represent a tree already.
>
> Thanks,
>
> Dave
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en