Reputation: 415
I would like to write a rule that tells me if a person passed a subject. The minimum grade to pass is 40%. I have successfully written a predicate that tells me if the person passed, however I am confused as to how I would get it to also give me person/subject pairs. i.e. julie/history. Thank you in advance.
% 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.
Upvotes: 0
Views: 1152
Reputation: 58324
Modify your passed
predicate to provide a Person
and Subj
pair that passes:
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).
Then collect all of them with findall/3
:
passing(PassingStudents) :-
findall(Person/Subj, passed(Person, Subj), PassingStudents).
Upvotes: 1
Reputation: 7229
Your fact database is designed in such a way that one person could only participate in one subject (otherwise there is no way to know what marks are for what subject).
With this in mind, here is a simple modification of your code:
passed(Person, Subj):-
studies(Person, Subj),
marks(Person, Work, Exam),
Perc is Work*0.25 + Exam*0.75,
Perc >= 40.
Test run:
?- passed(X, Y).
X = julie,
Y = history ;
false.
Upvotes: 1