Prolog membership predicate

2020-04-15 02:10发布

问题:

I need to write a Prolog predicate that avoids redundant answers with items occurring multiple times in the list at hand, as shown in the following sample queries:

?- member(a, [a, b, a]).
true
?- member(X, [a, b, a]).
X = a ;
X = b ;
false.
?- member(X, [a, b, a, c, a, d, b]).
X = a ;
X = b ;
X = c ;
X = d ;
false.

I know that the following would output all of them regardless of the repeats:

member(X, [X|_]).
member(X, [_|T]) :- member(X, T).

回答1:

Compare both clauses, are there cases, where both apply?

member(X, [X|_T]).
member(X, [_Y| T]) :- member(X, T).

Just compare both clauses to each other. Or, let Prolog do it for you:

?- member(X, [X|_T]) = member(X, [_Y| T]).
   X = _Y, _T = T.

So the _Y and X must be the same. When are they the same? Always, if the first clause is true! So we need to exclude that case by adding a further condition that in the second clause, they must be different.

memberd(X, [X|_T]).
memberd(X, [Y| T]) :- dif(X,Y), memberd(X, T).
?- member(X, [a, b, a, c, a, d, b]).
   X = a
;  X = b
;  X = a
;  X = c
;  X = a
;  X = d
;  X = b
;  false.
?- memberd(X, [a, b, a, c, a, d, b]).
   X = a
;  X = b
;  X = c
;  X = d
;  false.
?- memberd(a, [a, b, a, c, a, d, b]).
   true
;  false.

The last query can be improved using library(reif) which I use in Scryer, but which is also available for SICStus|SWI:

:- use_module(library(reif)).
memberd(E, [X|Xs]) :-
   if_(E = X, true, memberd(E, Xs) ).

?- memberd(a, [a, b, a, c, a, d, b]).
   true.


标签: list prolog