我有以下知识库
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).
我想写一个查询,允许团队最多的球队对阵战胜最少的球队,两队都在同一区域,而主队是胜利最多的球队
我有以下
canPlay(X,Y). Y can play X
canPlay(X,Y):-zone(zone(X)=:=Y). Y can play X, if Y zone == X
它不起作用。
答案 0 :(得分:0)
我想我明白你要做什么。你试图将最好的得分球队与得分最差的球队配对,但只能在同一区域内。这就是我接近它的方式:
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
这是一个很好的第一次尝试,但它会产生所有的组合,而不仅仅是那两个队伍被胜利恰当匹配的组合。
?- setof(X-Y, canPlay(X, Y), Pairings).
Pairings = [conneticut-duke, conneticut-villanova, duke-conneticut,
duke-villanova, villanova-conneticut, villanova-duke].
我并不完全了解您的其他要求,但希望这足以让您走上正轨。
答案 1 :(得分:0)
我认为这应符合您的要求
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).