在 Prolog 中编写谓词 [英] Writing predicate in Prolog

查看:45
本文介绍了在 Prolog 中编写谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有那些谓词:

          % 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:找出所有不学物理或化学的学生的名字.我不知道怎么写.我觉得应该是有删减的东西.

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 .

推荐答案

Not 在 Prolog 中有一个奇怪的语法,故意突出显示可能与非常人们期待.如果您有兴趣,请参阅 CWA.

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.

操作符是 \+,从句法上讲,这是平庸的:当您知道 goal 为假时,只需在 goal 前面加上它的前缀即可得到真,反之亦然.

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.

那么你的作业可以是:

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

看看你是否可以设计出 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)

(not A) and (not B) = not (A or B)

编辑在CWA下,我们可以使用否定作为失败.这就是 Prolog 实现 \+

edit under CWA, we can use negation as failure. That's the way Prolog implements \+

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

添加更正

\+ 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, _)).

我们可以写

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

noPhysicsNorChemistryStudents(Name, Department, Year, Town) :-
  student(_, Name, Town, _),
  study(Name, Department, Year),
  noPhysicsNorChemistry(Name).

这篇关于在 Prolog 中编写谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆