我有这些谓词:
% 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 .
答案 0 :(得分:1)
Not 有一个奇怪的语法,目的是突出显示它可能非常与人们期望的不同。如果您有兴趣,请参阅CWA。
运算符为\+,从语法上说它是平庸的:只需在目标前加上目标,当你知道目标为false时,获得一个真值,反之亦然。
然后你的作业可以是:
noPhysicsNorChemistryStudents(Name , Department , Year , Town) :-
student(_, Name, Town, _),
\+ ( AnyCondition ).
看看你是否可以设计AnyCondition公式,肯定会使用study(名称,部门,年份)。您可以应用布尔代数进行分解:
(不是A)和(不是B)=不是(A或B)
在CWA下编辑,我们可以使用否定作为失败。这就是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).