Reputation: 93
There are facts like :
student(ram, cs). // ram is student of cs branch
student(kiri,it).
student(akshay,cs).
student(sanjay,me).
I want to write a rule to find out the classmates in any branch AND a query to list out students in a branch say cs. Please help.
what query I had to run if i had to find classmates of akshay?
Upvotes: 0
Views: 348
Reputation: 60014
this being a follow up of this previous question, let's keep on the same mood...
classmates(Classmates) :-
aggregate(set(P), B^Q^(student(P,B), student(Q,B), P\=Q), Classmates).
yields
?- classmates(L).
L = [akshay, ram].
Upvotes: 0
Reputation: 21972
Two students are classmates if they are participating the same course.
classmates(X, Y) :- student(X, A), student(Y, A), X @< Y.
@</2
here is for suppressing duplicates. I.e it is enough to have only (A,B)
without (B,A)
, (A,A)
and (B,B)
.
?- classmates(X, Y).
X = akshay,
Y = ram ;
false.
To list out all students in a branch cs
:
?- student(X, cs).
X = ram ;
X = akshay.
Upvotes: 2