I'm struggling to get a decent result,
I have some friends,
friend(a,b).
friend(a,b2).
friend(a,b3).
friend(b,c).
friend(c,d).
friend(d,e).
friend(e,f).
Using findall(X,friend(a,X),List)
I'm getting all direct friends of a
List=[b,b2,b3].
For example, I want to get a list of 3 level friends of a
, for example, I want direct friends of a
, the friends of friends of a
(it means friends of b
, b2
, b3
) and the friends of c
. Getting the list:
List=[b,b2,b3,c,d].
I'm trying everything. I can only get direct friends or all friends of friends.
Help!!
The following stands for a 3-level friendship. As said in comments, if you want more, you might want to look for recursion.
% my friend is my friend
friend_of_friend(X,Y):-
friend(X,Y).
% the friend of my friend is my friend
friend_of_friend(X,Y):-
friend(X,Z),
friend(Z,Y).
% the friend of the friend of my friend is my friend
friend_of_friend(X,Y):-
friend(X,A),
friend(A,B),
friend(B,Y).
Then
findall(X, friend_of_friend(a,X), List).
Gives:
List = [b, b2, b3, c, d]
This would stand for infinite recursive friendship:
recursive_friend(X,Y):-
friend(X,Y).
recursive_friend(X,Y):-
friend(X,Z),
recursive_friend(Z,Y).
And give:
List = [b, b2, b3, c, d, e, f]