I have following knowledge base
team_name(1, conneticut).
team_name(2, duke).
team_name(3, memphis).
team_name(4, villanova).
team_name(5,gonzaga).
win_lose(1,22,2).
win_lose(2,24,1).
win_lose(3,23,2).
win_lose(4,20,2).
win_lose(5,21,3).
zone(west,5).
zone(south,3).
zone(east,1).
zone(east,2).
zone(east,4).
I want to write a query that allows the team with most wing play against the team with the least wins, which both are in the same zone, and the home team is the one with most wins
I have the following
canPlay(X,Y). Y can play X
canPlay(X,Y):-zone(zone(X)=:=Y). Y can play X, if Y zone == X
it does not work.
I think I understand what you're trying to do. You're trying to pair up the best scoring team with the worst scoring team, but only within the same zone. This is how I would approach it:
canPlay(X, Y) :-
team_name(XId, X), team_name(YId, Y), % let's get team X and team Y
X \= Y, % make sure they aren't the same
zone(Zone, XId), zone(Zone, YId). % but make sure they are in the same zone
This is a good first stab but it's going to generate all the combinations, not just the ones where the two teams are appropriately matched by wins.
?- setof(X-Y, canPlay(X, Y), Pairings).
Pairings = [conneticut-duke, conneticut-villanova, duke-conneticut,
duke-villanova, villanova-conneticut, villanova-duke].
I don't fully understand the rest of your requirements, but hopefully this is enough to get you on the right track.
I think this should fit your requirements
canPlay(Home, Guest) :-
% get different Zones
setof(Zone, Team^zone(Zone, Team), Zones),
% in a Zone
member(Zone, Zones),
% get ordered list
setof(Win-Team, Lost^(zone(Zone, Team), win_lose(Team, Win, Lost)), Standings),
% take first and last
append([[_-HomeId],_,[_-GuestId]], Standings),
team_name(HomeId, Home),
team_name(GuestId, Guest).