Reputation: 337
I have those predicate:
% 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).
I need to write predicate noPhysicsNorChemistryStudents(Name , Department , Year , Town)/4
:
find all students' name who not learn physics or chemistry.
I don't know how to write it. I think it should be something with cut.
% Signature: noPhysicsNorChemistryStudents(Name , Department , Year , Town)/4
Why this is not true? :
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 .
Upvotes: 1
Views: 2049
Reputation: 60034
Not in Prolog has a weird syntax, on purpose to highlight that could be very different from what people expect. See CWA if you are interested.
The operator is \+, and syntactically it's banal: just prefix a goal with it to get a true when what you know that goal is false, and viceversa.
Then your assignment could read:
noPhysicsNorChemistryStudents(Name , Department , Year , Town) :-
student(_, Name, Town, _),
\+ ( AnyCondition ).
See if you can devise the AnyCondition formula, that surely use study(Name, Department, Year). You could apply Boolean algebra to factorize:
(not A) and (not B) = not (A or B)
edit under CWA, we can use negation as failure. That's the way Prolog implements \+
\+ G :- call(G), !, fail.
add to correct
\+ G.
Should be clear now that if the predicate, with \+ allowed, would be like
noPhysicsNorChemistryStudents(Name, Department, Year, Town) :-
student(_, Name, Town, _),
study(Name, Department, Year),
\+ (study(Name, physics, _) ; study(Name, chemistry, _)).
we can write
noPhysicsNorChemistry(Name) :-
( study(Name, physics, _) ; study(Name, chemistry, _) ), !, fail.
noPhysicsNorChemistry(_).
noPhysicsNorChemistryStudents(Name, Department, Year, Town) :-
student(_, Name, Town, _),
study(Name, Department, Year),
noPhysicsNorChemistry(Name).
Upvotes: 1