0

I'm am trying to build a list that lists all the friends from and to two people in database. The problem I'm running into is that I get stuck in a infinite loop. This is what i have so far.

is_friends(From,To):- friend(From,To).
is_friends(From,To):- friend(From,Z), is_friends(Z,To).

Here is the database i'm working with,

friend(christian,margaret).
friend(christian,jas). 
friend(christian,todd).
friend(christian,ji).
friend(christian,geener).

friend(todd,christian).
friend(todd,susan).

friend(susan,todd).

friend(jas,christian).
friend(jas,geener).
friend(jas,clark).

friend(geener,christian).
friend(geener,jas).
friend(geener,ji).

friend(clark,pat).

friend(pat,mike).
friend(pat,clark).

friend(margaret,christian).

friend(ji,christian).
friend(ji,geener).

I guess i'm wondering is there a way for prolog to remember that i already checked a database and can move on to the next one?

This is what my output is supposed to look like

is_friends(From, To).
L = [christian, jas, clark).
christian
  • 105
  • 6
  • What makes you think this has an *infinite loop* (which is pretty hard for a language that doesn't have loops)? – Scott Hunter Nov 25 '17 at 03:22
  • well i traced it. It gets stuck on the first fact. It keep switching from (christian, margaret) to (margaret, christian). I never move to (christian, jas). – christian Nov 25 '17 at 03:30
  • @christian use a list , and to check that a member is never visited twice, you can make use of `\+ member/2` predicate. – Luai Ghunim Nov 25 '17 at 03:32
  • that interesting, this is sort of like a graph then and i have to keep track of where i have been. – christian Nov 25 '17 at 03:55
  • @christian You have both un-intantiated variables. It will have each possible solution . – Luai Ghunim Nov 25 '17 at 04:06
  • Look here `friend(jas,christian).` and `friend(christian,jas).` btw what you expect is not correct because christian is friend with `jas` but `jas` have 3 other friends aswell – Luai Ghunim Nov 25 '17 at 04:08
  • I found this question really interesting, The question is about the concept of Mutual friends.! X is mutual friends with Y if X is friend of Z and Z is friend of Y. The main problem is a mutual friend can be found on the first layer (both are friends) very easily. even in first layer( X - Z and Z-Y). but as it goes ahead. it is veryy hard. I am really interested in what will be the solution.! – Jay Joshi Nov 25 '17 at 11:42
  • How does the query `is_friend(From, To)` result in something that looks like `L = [christian, jas, clark]`? Where is `L`? – lurker Nov 25 '17 at 16:46
  • It looks like your `friend` relationship is directional since you explicitly defining both direction, correct? In other words, it's possible for `friend(bob, sally)` to be true, but not `friend(sally, bob)`? If that's not the case, then you don't need the extra converse facts and you should have a predicate handle the symmetry. For your "desired result", it looks like you want what the "network" of friends looks like from one person to another. If you want a list result, you need to include a list argument. This is a classic graph traversal problem, which has been covered many times here. – lurker Nov 25 '17 at 16:59
  • See, for example, [Graph Path Search With Cyclic Path](https://stackoverflow.com/questions/27180060/prolog-graph-path-search-with-cyclic-path). In this case `friend` is like `edge` and `path` is like `is_friends`. Do a little searching here on `[prolog] graph path` for example. – lurker Nov 25 '17 at 17:01
  • *I guess i'm wondering is there a way for prolog to remember that i already checked a database and can move on to the next one?* ... using a list argument that collects who you've already considered in the friend chain. – lurker Nov 25 '17 at 17:02
  • @christian, Did you get the answer of the question.? Or you are still looking for one..? – Jay Joshi Dec 02 '17 at 12:03

0 Answers0