0
student(ram,cse).
student(kirat,com).
student(ajay,cse).
student(amit,cmt).

studies(cmt,uc).
studies(cmt,pm).
studies(com,pm).
studies(com,aplc).
studies(com,ads).
studies(cse,aplc).
studies(cse,pm).
studies(cse,algorithms).
studies(cmt,pm).

classmate(X,Y):-student(X,Y).

I need to create the rule for "To count number of modules in all i.e. cse, cmt and com."

When I just give cse , cmt or com then it will show how many number of modules are there. These modules are stored in the given facts of studies how would i make a rule for that.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135

2 Answers2

2

Two issues in the solutions pointed by Shevliaskovic and mbratch. In the first case, aggregate_all /3 is not a standard predicate, making that solution less portable. But using setof/3 in the way shown will not give the expected results due to the anonymous variable:

?- setof(M, studies(M, _), ListOfModules), length(ListOfModules, Num).
ListOfModules = [com],
Num = 1 ;
ListOfModules = [cse],
Num = 1 ;
ListOfModules = [com, cse],
Num = 2 ;
ListOfModules = [cmt, com, cse],
Num = 3 ;
ListOfModules = [cmt],
Num = 1.

To make the problem more clear, we can replace the anonymous variable by a named variable:

?- setof(M, studies(M, A), ListOfModules), length(ListOfModules, Num).
A = ads,
ListOfModules = [com],
Num = 1 ;
A = algorithms,
ListOfModules = [cse],
Num = 1 ;
A = aplc,
ListOfModules = [com, cse],
Num = 2 ;
A = pm,
ListOfModules = [cmt, com, cse],
Num = 3 ;
A = uc,
ListOfModules = [cmt],
Num = 1.

Correcting this issue is easy. The solution is to explicitly qualify the second argument of studies/2 so that we don't get a solution for each value of the second argument:

?- setof(M, A^studies(M, A), ListOfModules), length(ListOfModules, Num).
ListOfModules = [cmt, com, cse],
Num = 3.
Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
1

aggregate_all(count, studies(cse,_), Count).

4 ?- aggregate_all(count, studies(cse,_), Count).
Count = 3.

It works the same with cmt, com.

As @mbratch said, this will not show the count of the unique modules, but the total count of them.

Shevliaskovic
  • 1,562
  • 4
  • 26
  • 43
  • 2
    This counts how many times the one module `cse` is used. I think @user2334012 wants to know how many unique modules exist. So it might need to be `setof(M, studies(M, _), ListOfModules), length(ListOfModules, Num).` and `Num` will be the number of unique modules. – lurker Nov 19 '13 at 22:27
  • It might. I'm not sure what he is trying to say with ' show how many number of modules are there' exactly – Shevliaskovic Nov 19 '13 at 22:29
  • Thanks @Shevliaskovic Thank u vry much. It works. But i wanted to ask you i am all new to this that the solution you have provided is this a query or a Rule – user2334012 Nov 19 '13 at 22:37
  • The thing is I wanted to show how many modules i.e. pm,aplc,algorithm are there in the selected branch i.e. com,cse,cmt. – user2334012 Nov 19 '13 at 22:39
  • what is the query for the "branches which studies both PM and APLC" . – user2334012 Nov 19 '13 at 23:06
  • `1?- studies(X,pm),studies(X,aplc). X = com ; X = cse ;` – Shevliaskovic Nov 20 '13 at 09:28