I suggest moving friendship outside of the objects themselves.

(defn person [name] {:name name})

(def bob (person "Bob"))
(def bill (person "Bill))
(def *friends* {bob bill})

now friends is a map of who likes whom. Then you can implement

(defn friend? [p1 p2] (or (= p1 (*friends* p2)) (= p2 (*friends* p1)))

As I wrote this, I see Stuart wrote a ref based version. Both work.  
The important point is make the graph of friendship separate from the  
nodes. Any sort of graph and edge based representation should work for  
you.

Cheers,
-Mark

On Jan 10, 2009, at 7:39 PM, CuppoJava 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to