【问题标题】:Truth and Lier-teller Prolog真相与说谎者序言
【发布时间】:2017-09-19 09:53:33
【问题描述】:

我真的是序言的新手,我得到了以下问题。这实际上是一个额外的挑战课程,但我什至不知道要开始。我会感谢任何想法或小例子(更好)。

在太平洋的一个岛上,有两种居民:总是说真话的人和总是说谎的人。然而,两者都深刻地意识到命题微积分。到达十字路口,您会发现其中一个居民,但您不知道他/她属于哪个部落。您还知道,其中一种方式将安全地带您到酒店,另一种方式将您带到致命的峡谷。知道原住民只会对一个问题回答“是”或“否”。在 PROLOG 中开发一个程序,以找到一个命题演算句子,让您选择正确的安全返回方式,无论问题是向说谎者部落的居民还是说真话者提出问题。

【问题讨论】:

  • 您应该尝试在 Google 上搜索“序言逻辑谜题”以查看一些示例。

标签: prolog logic


【解决方案1】:

你可以做的是:

% initialize which way is safe
isSafe(left).

% predicates for truth and lie tellers
truthTeller(X) :- X.
lieTeller(X) :- \+ X.
wouldTheOtherSay(X) :- truthTeller(lieTeller(X)).

那么你只需要问:

?- willTheOtherSay(isSafe(left)).

这将永远是一个谎言,所以如果答案是yes,你应该正确,反之亦然


编辑

这是另一个版本:

% initialize which way is safe
isSafe(left).

% initialize who is a truth-teller & who is a lie-teller
truthTeller(dean).
lieTeller(sean).

% a truth-teller would say Y is true if Y is true
% a lie-teller would say Y is true if Y is false
wouldSay(X,Y) :- 
   truthTeller(X),Y;
   lieTeller(X),\+Y.

% a truth-teller would say the other would say Y is true if Y is false
% a lie-teller would lie and say the other would say Y is true if Y is false
wouldTheOtherSay(X,Y) :-
   truthTeller(X),lieTeller(Z),wouldSay(Z,Y);
   lieTeller(X),truthTeller(Z),wouldSay(Z,\+Y).

% this is so you don't get true then false -- we keep the first result
onceWouldTheOtherSay(X,Y) :- once(wouldTheOtherSay(X,Y)).

然后:

?- onceWouldTheOtherSay(sean,isSafe(left)).
?- onceWouldTheOtherSay(dean,isSafe(left)).

会给你no,和:

?- onceWouldTheOtherSay(sean,isSafe(right)).
?- onceWouldTheOtherSay(dean,isSafe(right)).

会给你yes

尽管如此,lie*truth 等于 truth*lie 等于 lie,你应该反过来。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多