编辑:我错过了“eclipse”标签,所以也许这不是你要找的答案。它可以在普通的 Prolog 中工作,即 SWI。
所以你确实有参与者的枚举,至少我们不需要推断。从它开始:
- 船只:“Aetos”、“Ostria”、“Batis”、“Albatross”、“Thiella”
- 男子:“Miltos”、“Takis”、“Argiris”、“Xronis”、“Foibos”
- 女性:“Maro”、“Olga”、“Basia”、“Magda”、“Xara”
- 次:“16”、“19”、“21”、“24”、“27”
规则:以时间为价值!:
1 Argiris = Ostria,
2 Magda \= Miltos, Magda \= Takis,
Xronis \= Albatross, Maro \= Albatross, Xronis \= 24, Maro \= 24,
%% does that mean they were together in one boat??
3 Olga = Batis, Aetos \= 27,
4 ( Thiella = Miltos ; Thiella = Takis) , Thiella \= Basia,
5 Xara = 16, Olga > Magda,
6 Takis = 21
用于select/2
域选择:
select([A|B], C):- select(A,C,C2), select(B,C2).
select([],_).
我们准备开始编码。最明确的陈述首先出现。在定义之前不要测试任何变量,这一点很重要:
sailing(_Vars):-
Dom = [16,19,21,24,27], %% the domain
Takis = 21,
select( [Takis, Miltos, Argiris, Xronis, Foibos], Dom), Xronis \= 24,
Xara = 16,
select( [Xara, Maro, Magda, Olga, Basia], Dom), Maro \= 24,
Magda \= Takis, Magda \= Miltos, Olga > Magda,
Olga = Batis, Argiris = Ostria,
select( [Batis, Ostria, Aetos, Albatross, Thiella], Dom), Aetos \= 27,
Thiella \= Basia,
(Thiella = Miltos ; Thiella = Takis),
Xronis \= Albatross, Maro \= Albatross.
这给了我 57 个解决方案。所以也许Xronis
并且Maro
确实需要放在同一条船上。试试看。
如何报告您的答案是另一个问题。上面显示了求解器。
一种方法是调整上面的代码,例如
solve([Takis, Miltos, Argiris, Xronis, Foibos]) :- ... .
并将其称为?- solve(Men)
,以查看每个 的时间分配Men
。另一种是将代码更改为例如
pair( A-B, A, B).
solve(Result) :-
... ,
keysort( [Takis-'Takis', Miltos-'Miltos', Argiris-'Argiris',
Xronis-'Xronis', Foibos-'Foibos'], TM),
maplist( pair,TM,Times,Men),
keysort( [Xara-'Xara', Maro-'Maro', Magda-'Magda',
Olga-'Olga', Basia-'Basia'], TW),
maplist( pair,TW,Times,Women),
... ,
maplist( pair,TV,Times,Vessels),
Result = [Vessels,Men,Women,Times],
maplist( writeln, Result).
如果您还想将四个列表转换Result
为一个组合数据的转置列表,您也可以通过更多maplist/4
调用来完成此操作,例如
maplist( pair,VM,Vessels,Men), maplist( pair,WT,Women,Times),
maplist( quad,Result,VM,WT).
quad( (A,B,C,D), A-B, ... ).