I think there are two basic ways to do this. One is to use one of the mutable Clojure types mentioned above (or any mutable Java type) to make an actually circular structure, as you describe. For instance, you could accomplish (almost) what you want if :parent is an atom.
The other way is to give up on true circularity, and use an auxiliary map. For instance, suppose that each node also had a unique :name, and :parent and :children gave the *names* of the parent and children. Then, you maintain a separate map from names to nodes. When you want the actual parent or children of a node, you directly look up the names of these nodes, and then translate these to nodes using the auxiliary map. Of course, you can easily wrap this behavior in some auxiliary functions. The main downside of this approach is that you have to pass the auxiliary map around everywhere (you can't stuff it into the nodes, since this would again require mutability). A middle-ground would be to use a Java (mutable) HashMap for this purpose, which could be stored inside each node. Cheers, Jason On Feb 12, 12:32 am, timc <timgcl...@gmail.com> wrote: > I'm new to Clojure so this may be a stupid question. > > I want to make a tree out of these things: > > (defstruct treeNode :parent :children :data) > > so that every node knows which node is its parent and which nodes are > its children. > > But, given that these things are immutable, I am baffled as to how the > tree may be constructed - in particular how it's possible to make > immutable things that 'point at' each other. > > Any guidance on this would be greatly appreciated. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---