Question

so I have a set of facts:

course(cs61,  "intro to cs")
...
course(cs62b, "data structure")
...
grade(adam,       cs61, spring11, "A")
...
grade(chrisitian, cs61, fall2010, "A-")

I need to define a predicate good_standing(Student) to determine whether the Student got A in every class he took. I must use 2 different approaches:

  1. use setof/3
  2. use NAF

on the 1st. I have tried to figure out get Result_list: the set of students that got A from every class he took. and then call member(Student, Result_list). But I don't know what to get the Result_list since the syntax is a bit strange to me.

Was it helpful?

Solution

1) For the NAF solution you should keep in mind that the good-standing student is the one with no grades lower than A, i.e., the one such that there is no course he/she took and the grade is lower than A. We also require this person to take at least one course.

good_standing(Student) :- grade(Student, _,_,_), 
                          \+ (grade(Student, Course, _, Grade), 
                              lower(Grade,"A")).

with lower/2 defining relation between the grades:

lower("A-","A").
lower("B","A").
...

The solution above works if students get only one grade per course.

2) For the setof solution you can find all grades per student and check that they are A.

grade(Student, Grade) :- grade(Student, _,_,Grade).
good_standing(Student) :- setof(Grade, grade(Student,Grade), ["A"]).

Alternatively, as suggested by false we can make the existential quantification explicit within setof:

good_standing(Student) :- setof(Grade, 
                                Course^Period^grade(Student,Course,Period,Grade),
                                ["A"]).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top