Joshua Manero
Joshua Manero

Reputation: 11

Creating a prolog rule with count

mother(X, Y):- child(Y, X), female(X).

How would I have code to find out whether or not X is the mother of at least 3 children? Would I have to use some sort of aggregate to see how many times child(Y,X) would happen?

Upvotes: 1

Views: 173

Answers (1)

max66
max66

Reputation: 66230

Not sure to understand what do you want but I suppose that the following example could help

motherOf3OrMore(X) :-
  female(X),
  findall(Y, child(Y, X), L),
  length(L, N),
  N >= 3.

If the minimum number of childs isn't a fixed number (3) you can pass it as a parameter as follows

motherOfMore(X, N) :-
  female(X),
  findall(Y, child(Y, X), L),
  length(L, M),
  M >= N.

Upvotes: 1

Related Questions