projog

3.113. memberchk(E, L) - checks is a term is a member of a list.

memberchk(E, L) succeeds if E is a member of the list L. No attempt is made to retry the goal during backtracking - so if E appears multiple times in L only the first occurrence will be matched.

Examples

?- memberchk(a, [a,b,c]).

yes

?- memberchk(b, [a,b,c]).

yes

?- memberchk(c, [a,b,c]).

yes

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

no

?- memberchk(d, []).

no

?- memberchk(X, []).

no

?- memberchk([], []).

no

?- memberchk(X, [a,b,c|d]).
X = a

yes

?- memberchk(a, [a,b,c|d]).

yes

?- memberchk(b, [a,b,c|d]).

yes

?- memberchk(c, [a,b,c|d]).

yes

?- memberchk(d, [a,b,c|d]).

Expected empty list or variable but got: ATOM with value: d

?- memberchk(z, [a,b,c|d]).

Expected empty list or variable but got: ATOM with value: d

?- memberchk(X, [a,b,c]).
X = a

yes

?- memberchk(p(X,b), [p(a,b), p(z,Y), p(x(Y), Y)]).
X = a
Y = UNINSTANTIATED VARIABLE

yes

?- memberchk(p(a,X),[p(x,y),b,p(Y,Y)]).
X = a
Y = a

yes

?- memberchk(a, X).
X = [a|_]

yes

?- memberchk(p(a,X),a).

Expected list or empty list but got: ATOM with value: a

?- memberchk(something, [something|_]).

yes

?- memberchk(anything, [something|_]).

yes

?- memberchk(anything, [something|X]).
X = [anything|_]

yes