0

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?

false
  • 10,264
  • 13
  • 101
  • 209
Hacker688
  • 93
  • 8

2 Answers2

2

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.
0

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].
Community
  • 1
  • 1
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • I think its wrong..as if suppose we have a fact student(raju,it),then it will give [akshay,ram,raju,kiri] as result but we can clearly see akshay and ram are not classmates of raju and kiri. – Hacker688 Nov 03 '13 at 14:46
  • @Hacker688: yes, I think so.. maybe I'll delete this answer ... but consider that a classmate is simply a person that has *any* other person studying the same course. Then, apart sanjay, all other students *are* classmates. Maybe, not what you're looking for... – CapelliC Nov 03 '13 at 14:51
  • :ya,You are right..but raju and kiri study differnet courses from akshay and ram.hence cnt be their classmate.Becose we assume those to be classmates that study same course – Hacker688 Nov 03 '13 at 15:01
  • Yes, I was suggesting to try to understand library(aggregate). Try to play with **quantification**, those variables with **^** – CapelliC Nov 03 '13 at 15:28