我想写一条规则,告诉我一个人是否通过了一个主题。通过的最低成绩是40%。我已经成功地写了一个谓词,告诉我这个人是否通过,但我很困惑,我怎么会得到它也给我个人/主题对。即朱莉/历史。提前谢谢。
% studies( Person, Subject)
% -------------------------
studies( julie, history).
studies( pete, chemistry).
% marks( Person, CourseWork, Exam)
% --------------------------------
marks( julie, 77, 63).
marks( pete, 55, 21).
passed(Person,_Subj):-
%get student work and exam grades
marks(Person, Work, Exam),
%calculate the final student grade
Perc is Work*0.25 + Exam*0.75,
%see if percentage is over 40%
Perc >= 40.
答案 0 :(得分:1)
您的事实数据库的设计方式使得一个人只能参与一个主题(否则无法知道什么标记是针对哪个主题)。
考虑到这一点,这里是对代码的简单修改:
passed(Person, Subj):-
studies(Person, Subj),
marks(Person, Work, Exam),
Perc is Work*0.25 + Exam*0.75,
Perc >= 40.
试运行:
?- passed(X, Y).
X = julie,
Y = history ;
false.
答案 1 :(得分:1)
修改您的passed
谓词,以提供通过的Person
和Subj
对:
passed(Person, Subj):-
%get student work and exam grades
marks(Person, Work, Exam),
%calculate the final student grade
Perc is Work*0.25 + Exam*0.75,
%see if percentage is over 40%
Perc >= 40,
studies(Person, Subj).
然后使用findall/3
收集所有内容:
passing(PassingStudents) :-
findall(Person/Subj, passed(Person, Subj), PassingStudents).