首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swi prolog求解

swi prolog求解
EN

Stack Overflow用户
提问于 2013-04-02 18:45:59
回答 1查看 2.3K关注 0票数 0

我正在使用windows7的SWI Prolog,我的作业之一是一个基本的逻辑难题。我们已经得到了一个独立问题的样本解决方案-包括它的源代码。

这是“打成平手”的问题。然而,我不知道如何使用Solve:- predicate获得结果。在查看.pl文件之后,我是否需要向控制台输入特定的命令或类似的东西?

谢谢你的帮助。

源代码(不是我写的代码):

代码语言:javascript
复制
% Problem #1, "It's a tie", Dell Logic Puzzles, October 1999
% Each man (mr so-and-so) got a tie from a relative.
tie(cupids).
tie(happy_faces).
tie(leprechauns).
tie(reindeer).

mr(crow).
mr(evans).
mr(hurley).
mr(speigler).

relative(daughter).
relative(father_in_law).
relative(sister).
relative(uncle).

solve :-
tie(CrowTie), tie(EvansTie), tie(HurleyTie), tie(SpeiglerTie),
all_different([CrowTie, EvansTie, HurleyTie, SpeiglerTie]),

relative(CrowRelative), relative(EvansRelative),
relative(HurleyRelative), relative(SpeiglerRelative),
all_different([CrowRelative, EvansRelative, HurleyRelative, SpeiglerRelative]),

Triples = [ [crow, CrowTie, CrowRelative],
            [evans, EvansTie, EvansRelative],
            [hurley, HurleyTie, HurleyRelative],
            [speigler, SpeiglerTie, SpeiglerRelative] ],

% 1. The tie with the grinning leprechauns wasn't a present from a daughter.
\+ member([_, leprechauns, daughter], Triples),

% 2. Mr. Crow's tie features neither the dancing reindeer nor the yellow happy faces.
\+ member([crow, reindeer, _], Triples),
\+ member([crow, happy_faces, _], Triples),

% 3. Mr. Speigler's tie wasn't a present from his uncle.
\+ member([speigler, _, uncle], Triples),

% 4. The tie with the yellow happy faces wasn't a gift from a sister.
\+ member([_, happy_faces, sister], Triples),

% 5. Mr Evans and Mr. Speigler own the tie with the grinning leprechauns
%    and the tie that was a present from a father-in-law, in some order.
( (member([evans, leprechauns, _], Triples),
   member([speigler, _, father_in_law], Triples)) ;

  (member([speigler, leprechauns, _], Triples),
   member([evans, _, father_in_law], Triples)) ),

% 6. Mr. Hurley received his flamboyant tie from his sister.
member([hurley, _, sister], Triples),

tell(crow, CrowTie, CrowRelative),
tell(evans, EvansTie, EvansRelative),
tell(hurley, HurleyTie, HurleyRelative),
tell(speigler, SpeiglerTie, SpeiglerRelative).

% Succeeds if all elements of the argument list are bound and different.
% Fails if any elements are unbound or equal to some other element.
all_different([H | T]) :- member(H, T), !, fail.
all_different([_ | T]) :- all_different(T).
all_different([_]).

tell(X, Y, Z) :-
write('Mr. '), write(X), write(' got the '), write(Y),
write(' tie from his '), write(Z), write('.'), nl.
EN

回答 1

Stack Overflow用户

发布于 2013-04-02 20:12:11

在查阅文件之后,只需键入谓词的名称,并将参数放在括号中(如果有),后跟一个句号。如果查询中有多个谓词,请用逗号分隔它们。在您的示例中(我将文件命名为solve.pl):

代码语言:javascript
复制
?- [solve].
% solve compiled 0.00 sec, 18 clauses
true.

?- solve.
Mr. crow got the cupids tie from his daughter.
Mr. evans got the leprechauns tie from his uncle.
Mr. hurley got the reindeer tie from his sister.
Mr. speigler got the happy_faces tie from his father_in_law.
true ;
false.

?- 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15762140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档