I have a list of persons
and parent
facts
person(nameA).
person(nameB).
person(nameC).
parent(parentname, children).
I need to write a rule hasnochild(X).
to iterates through all person
that has no child.
This is what I have wrote thus far.
hasnochild(X) :- parent(Z, X).
But it returns all the person's name who has a child. How do I get those who have no child?
Something like the whole list of person
minus off what I have wrote.
You have to try to unify Person (
X
in your question) with a person in the database, then succeed only if that person does not have children.E.g.:
The call to
person(X)
may be avoided if you know that the first argument toparent/2
is a person.