I'm having an issue with a part of my assignment. I'm supposed to write a predicate "friendly" which is supposed to be true when network member X is said to be friendly if X likes back everyone who likes him/her.
EDIT: In the below example, barry is friendly as the list of people that like barry is kara and barry likes kara back. Kara is NOT the right answer as the list of people that like Kara are Barry, Clark and Oliver BUT Kara only likes back Barry and Clark so Kara is not friendly.
eg of G = [person(kara, [barry, clark]),person(bruce, [clark, oliver]),person(barry, [kara, oliver]),person(clark, [oliver, kara]),person(oliver, [kara])]
What I have so far;
friendly(G, X):-
member_(person(X, _), G),
likers(G, X, L),
likes_all(G, X, L).
% to get the list of members who like X;
likers(G, X, [Y|T]) :-
likes(G, Y, X),
select_(Y, G, G2),
likers(G2, X, T).
likers([], _, []).
likers([], _, _).
likers(_, _, []).
% select is used to remove the person from the list once visited.
select_(X, [person(X, _)|T], T).
select_(X, [H|T], [H|R]) :-
select_(X, T, R).
% to check whether X likes all the list of people that like X;
likes_all(G, X, [H|T]):-
likes(G, X, H),
likes_all(G, X, T).
likes_all(_, _, []).
likes_all(G, [H|T], X):-
likes(G, H, X),
likes_all(G, T, X).
likes_all(_, [],_).
likes(G, X, Y):-
member_(person(X, L), G),
member_(Y, L).
member_(X, [X|_]).
member_(X, [_|T]) :-
member_(X, T).
My issue is it doesn't work properly. See sample output below.
So, I don't know what is wrong and how I'm supposed to do it. We are not allowed to use any built in predicates or control operators, so no !, ;, =, \=, +, etc, only pure prolog.
Any hint on moving forward is appreciated.
Output:
[debug] ?- friendly([person(kara, [barry, clark]),person(bruce, [clark, oliver]),person(barry, [kara, oliver]),person(clark, [oliver, kara]),person(oliver, [kara])], X).
X = kara ;
X = kara ;
X = kara ;
X = kara ;
X = kara ;
X = bruce ;
X = barry ;
X = barry ;
X = clark ;
X = clark ;
X = oliver ;
false.
I think my error is somewhere in likers function. Output of "likers":
?- likers([person(kara, [barry, clark]), person(bruce, [clark, oliver]), person(barry, [kara, oliver]), person(clark, [oliver, kara]), person(oliver, [kara])], kara, L).
L = [] ;
L = [barry] ;
L = [barry, clark] ;
L = [barry, clark, oliver] ;
L = [barry, oliver] ;
L = [barry, oliver, clark] ;
L = [clark] ;
L = [clark, barry] ;
L = [clark, barry, oliver] ;
L = [clark, oliver] ;
L = [clark, oliver, barry] ;
L = [oliver] ;
L = [oliver, barry] ;
L = [oliver, barry, clark] ;
L = [oliver, clark] ;
L = [oliver, clark, barry] ;
false.
In the above, the right answer would be L = [barry, clark, oliver] or one of the combinations. Is there a way to get that in pure prolog ?
likes/3
andmember_/2
does not exist). – damianodamianoG
representing ?? 2) For whatX
is supposedfriendly(G, X)
to succeed forG = [person(alice,[bob]),person(bob,[])]
?? – coderperson/2
clauses should probably be in the database, not passed in a list. – Tomas By