-1

I have facts :

studies(it, da).      // IT branch studies the module DA
studies(it, pm).
studies(ie, pm).
studies(it, plc).
studies(it, se).
studies(cs, plc).
studies(cs, da).
studies(cs, se).

I need to write queries for:

  1. How many subjects do cs students study?

  2. Which branch studies the subjects pm and plc?

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

1 Answers1

1
  1. aggregate_all(count, studies(cs, X), Count). (See Prolog count the number of times a predicate is true)

  2. studies(X, pm), studies(X, plc).

Community
  • 1
  • 1
Daniel Hershcovich
  • 3,751
  • 2
  • 30
  • 35
  • Can you plz tell what is difference between a rule and a query.Actually i am a newbie to prolog. – Hacker688 Nov 03 '13 at 12:55
  • @Hacker688 A rule has the form ` :- `, where `Head` names a predicate which takes from 0-n arguments which are either variables or ground terms and `Body` lays out a set of conditions which must be met if `Head` is to be true. We can read rule thus: 'Head is true if every statement in Body is true'. A fact is essentially a rule of the form ` :- true`. A query is entered at the top level, after the prompt `?-` and asks if a certain statement is true. Prolog tries to prove that the queried statement is true by testing whether it follows from the existing facts and rules. – Shon Nov 03 '13 at 16:07