1

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 .
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user1479376
  • 327
  • 1
  • 2
  • 10

1 Answers1

1

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).
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • thank you , but i forgot to tell that we are not allowed to use \+ (we can use \= , but i dont think it will work.. – user1479376 Jun 27 '12 at 08:37
  • see my edit, HTH, but you really need to read that CWA page on wikipedia. Especially the [PDF](http://www.mindswap.org/2005/OWLWorkshop/sub12.pdf) you find at bottom has an introductory part that explain simply what's the problem you need to solve in your assignment – CapelliC Jun 27 '12 at 09:55
  • call(Goal) means `prove` the goal you have as argument. Note that in call(*G*) is uppercase, a variable, can be any term (a `callable`) – CapelliC Jun 27 '12 at 10:39
  • i is not work to me.. can you pleaze convert it to what you suggest: – user1479376 Jun 27 '12 at 10:49
  • noPhysicsNorChemistryStudents(Name , Department , Year , Town) :- student(_, Name, Town, _), \+ study(Name , physics , Year ), \+ study(Name , chemistry , Year ). – user1479376 Jun 27 '12 at 10:49
  • I'm sorry, I forgot an important line in `not` implementation... See the edit – CapelliC Jun 28 '12 at 12:17