1

我有那些谓词:

          % Signature: student(ID, Name, Town , Age)/4
      % Purpose: student table information

      student(547457339, riki, beerSheva , 21).
      student(567588858, ron, telAviv , 22).
      student(343643636, vered, haifa , 23).
      student(555858587, guy, beerSheva , 24).
      student(769679696, smadar, telAviv , 25).


      % Signature: study(Name, Department , Year)/3
      % Purpose: study table information

      study(riki, computers , a).
      study(ron, mathematics , b).
      study(vered, computers , c).
      study(riki, physics , a).
      study(smadar, mathematics , c).
      study(guy, computers , b).


      % Signature: place(Department ,Building,  Capacity)/3
      % Purpose: place table information

      place(computers , alon , small).
      place(mathematics , markus , big).
      place(chemistry , gorovoy , big).
      place(riki, zonenfeld , medium).

我需要写谓词noPhysicsNorChemistryStudents(Name , Department , Year , Town)/4:找出所有没有学物理或化学的学生的名字。我不知道怎么写。我认为它应该是有切割的东西。

          % Signature: noPhysicsNorChemistryStudents(Name , Department , Year , Town)/4

为什么这不是真的?:

  noPhysicsNorChemistryStudents2(Name , Department , Year , Town) :-
  student(_, Name, Town, _), study(Name , Department , Year),
  pred1(Name , physics , Year ) , pred1(Name , chemistry , Year ).

  pred1(N,D ,Y):-  study(N , D , Y ) , ! , fail .
4

1 回答 1

1

Not in Prolog 有一个奇怪的语法,目的是为了强调这可能与人们所期望的非常不同。如果您有兴趣,请参阅CWA 。

运算符是\+,在语法上它是平庸的:只需在目标前面加上它,当你知道目标是错误的时候得到一个真实的目标,反之亦然。

那么你的作业可以是:

noPhysicsNorChemistryStudents(Name , Department , Year , Town) :-
   student(_, Name, Town, _),
   \+ ( AnyCondition ).

看看您是否可以设计肯定使用研究(姓名,部门,年份)的 AnyCondition 公式。您可以应用布尔代数分解:

(非 A) 和 (非 B) = 非 (A 或 B)

在 CWA 下编辑,我们可以使用negation as failure。这就是 Prolog 实现的方式 \+

\+ G :- call(G), !, fail.

添加到更正

\+ G.

现在应该清楚,如果允许 \+ 的谓词就像

noPhysicsNorChemistryStudents(Name, Department, Year, Town) :-
  student(_, Name, Town, _),
  study(Name, Department, Year),
  \+ (study(Name, physics, _) ; study(Name, chemistry, _)).

我们可以写

noPhysicsNorChemistry(Name) :-
  ( study(Name, physics, _) ; study(Name, chemistry, _) ), !, fail.
noPhysicsNorChemistry(_).

noPhysicsNorChemistryStudents(Name, Department, Year, Town) :-
  student(_, Name, Town, _),
  study(Name, Department, Year),
  noPhysicsNorChemistry(Name).
于 2012-06-27T08:12:44.390 回答