i try to solve a problem about lists but i can't... :(
PLAYER(TEAM,[PLAYERS WHO PLAY IN THAT TEAM]).
player(milan,[seedorf,zambrotta,gattuso]).
player(inter,[seedorf,ronaldo,zambrotta]).
player(realmadrid,[seedorf,zidane,ronaldo开发者_高级运维]).
and i try to write predicates that returns 2 Lists; P1 and P2...
find(P1,P2).
For example; if my goal is:
find([milan,inter],X). returns X:[seedorf,zambrotta].
players who played juventus and also inter
find([inter,realmadrid],X). returns X:[seedorf,ronaldo]
players who played inter and also realmadrid
find(X,[seedorf]). returns X:[juventus,inter,realmadrid]
teams that seedorf played
find(X,[seedorf,ronaldo]). returns X:[inter,realmadrid]
teams that seedorf and ronaldo played.
i try to solve this but i can't go further than that (and it is not even enough to try P2 list members find in player facts :( ) : MOREOVER I CANNOT USE "!" because of lecture restrictions.
find(P1,[P2|P2s]):-findall(X,player(X,P2),P1),find(P1,P2s).
Thanks A Lot....
As you accept both P1
and P2
as inputs, I suggest to break down both P1
and P2
by their structures to do recursion. One solution could be finding all team of a specific player and finding all players playing for a specific team:
find1([T], L) :- player(T, L).
find1([T|Ts], L) :- player(T, L0), find1(Ts, L1), intersection(L0, L1, L).
find2(L, [P]) :- findall(X, (player(X, Y), member(P, Y)), L).
find2(L, [P|Ps]) :- findall(X, (player(X, Y), member(P, Y)), L0), find2(L1, Ps), intersection(L0, L1, L).
find(X, Y) :- is_list(X), find1(X, Y).
find(X, Y) :- is_list(Y), find2(X, Y).
Hope it helps you somehow.
精彩评论