prolog - 提取信息

时间:2012-05-17 17:31:33

标签: list prolog

目前正在做一些PROLOG练习 - 对我来说非常陌生。我有以下知识库:

/* The structure of a subject teaching team takes the form:
team(Subject, Leader, Non_management_staff, Deputy).
Non_management_staff is a (possibly empty) list of teacher
structures and excludes the teacher structures for Leader and
Deputy.
teacher structures take the form:
teacher(Surname, Initial,
profile(Years_teaching,Second_subject,Club_supervision)).
Assume that each teacher has his or her team's Subject as their
main subject. */

team(computer_science,teacher(may,j,profile(20,ict,model_railways)),
[teacher(clarke,j,profile(32,ict,car_maintenance))],
teacher(hamm,p,profile(11,ict,science_club))).

team(maths,teacher(vorderly,c,profile(25,computer_science,chess)),
[teacher(o_connell,d,profile(10,music,orchestra)),
teacher(brankin,p,profile(20,home_economics,cookery_club))],
teacher(lynas,d,profile(10,pe,football))).

team(english,teacher(brewster,f,profile(30,french,french_society)),
[ ],
teacher(flaxman,j,profile(35,drama,debating_society))).

team(art,teacher(lawless,m,profile(20,english,film_club)),
[teacher(walker,k,profile(25,english,debating_society)),
teacher(brankin,i,profile(20,home_economics,writing)),
teacher(boyson,r,profile(30,english,writing))],
teacher(carthy,m,profile(20,music,orchestra))).

subject(X):- team(X,_,_,_).
leader(X) :- team(_,X,_,_).
deputy(X) :- team(_,_,_,X).

non_management(X) :-
    team(_,_,Non_management,_),
    member(X,Non_management).

exists(X) :-
    subject(X);  
    leader(X);
    deputy(X);
    non_management(X).

我现在必须写一条规则,其中(q)是教师A和教师B的首字母,其中教师A和教师B在不同的学科团队中,每个都有家政经济学作为他们的第二个 主题,每个都有姓Brankin。

我坚持如何比较知识库中的所有实体。在此之前,我只从单个实体中提取了值(在本例中,单个教师)。例如:

question1(Initial,Surname) :-
    exists(teacher(Surname,Initial,profile(_,english,_))).

任何帮助都非常感激。

1 个答案:

答案 0 :(得分:0)

您不需要明确地比较知识库中的所有实体 - 这是Prolog回答查询的方式中隐含的,无论它们是简单还是复杂。对于前几个标准,您可以说

team(W,X,Y,Z), team(J,K,L,I), W \= J.

这将通过回溯为您提供不同团队的所有可能组合。您可以使用

之类的内容扩展查询
member(A,Y), member(B,L), A=teacher(...), B=teacher(...)

处理其他标准。

相关问题