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をご覧ください。

演算子は\+であり、構文的には平凡です: ゴールの前にそれを付けるだけで、そのゴールが false であることがわかっているときに true を取得できます。逆の場合も同様です。

次に、割り当てを次のように読むことができます。

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

確実に study(Name, Department, Year) を使用する AnyCondition 式を考案できるかどうかを確認してください。ブール代数を適用して因数分解できます。

(not A) and (not B) = not (A or 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).
于 2012-06-27T08:12:44.390 に答える