member(E, L)
succeeds if E
is a member of the list L
. An attempt is made to retry the goal during backtracking - so it can be used to enumerate the members of a list.
Examples
?- member(a, [a,b,c]).
yes;
no
?- member(b, [a,b,c]).
yes;
no
?- member(c, [a,b,c]).
yes
?- member(d, [a,b,c]).
no
?- member(d, []).
no
?- member([], []).
no
?- member(X, [a,b,c]).
X = a
yes;
X = b
yes;
X = c
yes
?- member(p(X,b), [p(a,b), p(z,Y), p(x(Y), Y)]).
X = a
Y = UNINSTANTIATED VARIABLE
yes;
X = z
Y = b
yes;
X = x(b)
Y = b
yes
?- member(X, [a,b,c,a|d]).
X = a
yes;
X = b
yes;
X = c
yes;
X = a
yes
?- member(a, [a,b,c,a|a]).
yes;
yes
?- member(a, [a,b,c,a|d]).
yes;
yes
?- member(b, [a,b,c,a|d]).
yes;
no
?- member(c, [a,b,c,a|d]).
yes;
no
?- member(d, [a,b,c,a|d]).
no
?- member(z, [a,b,c,a|d]).
no
?- member(X, a).
no
?- member(X, p(a,b)).
no
?- member(X, 1).
no
?- member(X, 1.5).
no
?- member(a, [a,a,a|X]).
X = UNINSTANTIATED VARIABLE
yes;
X = UNINSTANTIATED VARIABLE
yes;
X = UNINSTANTIATED VARIABLE
yes;
X = [a|_]
yes;
X = [_,a|_]
yes;
X = [_,_,a|_]
yes;
X = [_,_,_,a|_]
yes
?- member(a, [a,b,c|X]).
X = UNINSTANTIATED VARIABLE
yes;
X = [a|_]
yes;
X = [_,a|_]
yes;
X = [_,_,a|_]
yes;
X = [_,_,_,a|_]
yes
?- member(d, [a,b,c|X]).
X = [d|_]
yes;
X = [_,d|_]
yes;
X = [_,_,d|_]
yes;
X = [_,_,_,d|_]
yes
?- member(a, X).
X = [a|_]
yes;
X = [_,a|_]
yes;
X = [_,_,a|_]
yes
?- member(X, Y).
X = UNINSTANTIATED VARIABLE
Y = [X|_]
yes;
X = UNINSTANTIATED VARIABLE
Y = [_,X|_]
yes;
X = UNINSTANTIATED VARIABLE
Y = [_,_,X|_]
yes
?- X=[a,b,c|Z], member(a,X).
X = [a,b,c|Z]
Z = UNINSTANTIATED VARIABLE
yes;
X = [a,b,c,a|_]
Z = [a|_]
yes;
X = [a,b,c,_,a|_]
Z = [_,a|_]
yes;
X = [a,b,c,_,_,a|_]
Z = [_,_,a|_]
yes
?- member(p(X),[p(a),p(b),p(c)|Z]).
X = a
Z = UNINSTANTIATED VARIABLE
yes;
X = b
Z = UNINSTANTIATED VARIABLE
yes;
X = c
Z = UNINSTANTIATED VARIABLE
yes;
X = UNINSTANTIATED VARIABLE
Z = [p(X)|_]
yes;
X = UNINSTANTIATED VARIABLE
Z = [_,p(X)|_]
yes;
X = UNINSTANTIATED VARIABLE
Z = [_,_,p(X)|_]
yes;
X = UNINSTANTIATED VARIABLE
Z = [_,_,_,p(X)|_]
yes