I want to check if a person lives in a specific house ismemberof(NAME, HOUSE) -> (true Or false once). My code is as below. The problem is it checks all the facts that having the same name that matches with the argument and returns multiple results which I don't want. How can I achieve this? Thank you.
% Facts:
member(alex, 19, house1).
member(alex, 19, house2).
member(lisa, 21, house3).
% Rules:
ismemberof(NAME, HOUSE) :-
member(NAME, _, HOUSE).
% Queries:
?- ismemberof(alex, house1).
% expected: true
% actual: true
false
You are looking for the cut goal (!).
You can either add it to the predicate: ismemberof(N,H) :- member(N,_,H),!.
or you add it to the query itself: ismemberof(alex,house1),!.
.
The cut operator essentially tells Prolog to not backtrack beyond the cut goal once it was crossed.
Since it is a red cut, you have to take care: The query ismemberof(alex, X).
will produce two results without cut and only one result with the cut. Therefore I would alter query and not the predicate if I want to check membership.