Prolog not unique elements in predicate call -
in prolog script, have defined:
mother(x,y) :- parent_of(x,y), female(x).
i want know if there mothers more 2 children, run:
mother(x,y), mother(x,z)
with result:
x = pam, y = m, m = bob
which has left me quite baffled.... figured if add
not(y = z)
this fix it, unsure why...
if execute query like
mother(x,y).
the result bring mothers have 2 children well.
so if database like
female(maria). female(irini). parent_of(maria,nick). parent_of(maria,dario). parent_of(irini,dewey).
and executed mother(x,y).
query, result bring back
1 ?- mother(x,y). x = maria, y = nick ; x = maria, y = dario ; x = irini, y = dewey.
so result have mother (maria) has 2 children.
if only want mother 2 children, should modify mother
query as:
mother(x,y) :- parent_of(x,y), parent_of(x,m), y \= m, female(x).
the result of query be:
3 ?- mother(x,y). x = maria, y = nick ; x = maria, y = dario ; false.
(false means prolog did't find more results).
Comments
Post a Comment