Hi, After (are-friends bill bob) they should be friends, so I tried @bob and don't see bill as a friend. why?
@bob {:friends #{#<Ref clojure.lang....@165ab39>}, :name "Bob"} -sun On Jan 10, 9:44 pm, Stuart Sierra <the.stuart.sie...@gmail.com> wrote: > Hi Patrick, > > Here's one way to do it: > > (defn new-person [name] > (ref {:name name, :friends #{}})) > > (defn are-friends [a b] > (dosync > (commute a assoc :friends (conj (:friends @a) b)) > (commute b assoc :friends (conj (:friends @b) a)))) > > (def bill (new-person "Bill")) > (def bob (new-person "Bob")) > > (are-friends bill bob) > > -Stuart Sierrra > > On Jan 10, 8:39 pm, CuppoJava <patrickli_2...@hotmail.com> wrote: > > > Hello, > > I'm stumped as to how I create a mutually referential data structure > > in Clojure. My compsci is fuzzy. I don't know if what I'm trying to do > > is possible. Any insight would be helpful. > > > I have a function that creates a Person, given his name and a list of > > friends. > > > (defn new_person [name & friends] > > {:name name > > :friends friends}) > > > But how do I create a mutually referential definition? > > > ie. what if Bob is Bill's friend, and Bill is also Bob's friend? > > I would have to do something like the following: (which is not > > allowed) > > > (def bob (new_person "Bob" bill)) <-Not allowed: forward reference. > > (def bill (new_person "Bill" bob)) > > > Thanks a lot for your help > > -Patrick --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---