projog

3.117. select(X,Y,Z) - removes an element from a list.

Attempts to unify Z with the result of removing an occurrence of X from the list represented by Y. An attempt is made to retry the goal during backtracking.

Examples

?- select(X,[h,e,l,l,o],Z).
X = h
Z = [e,l,l,o]

yes;
X = e
Z = [h,l,l,o]

yes;
X = l
Z = [h,e,l,o]

yes;
X = l
Z = [h,e,l,o]

yes;
X = o
Z = [h,e,l,l]

yes

?- select(l,[h,e,l,l,o],Z).
Z = [h,e,l,o]

yes;
Z = [h,e,l,o]

yes;

no

?- select(l,[h,e,l,l,o],[h,e,l,o]).

yes;

yes;

no

?- select(p(a,B),[p(X,q), p(a,X)],Z).
B = q
X = a
Z = [p(a, a)]

yes;
B = UNINSTANTIATED VARIABLE
X = UNINSTANTIATED VARIABLE
Z = [p(B, q)]

yes

?- select(a, Result, [x,y,z]).
Result = [a,x,y,z]

yes;
Result = [x,a,y,z]

yes;
Result = [x,y,a,z]

yes;
Result = [x,y,z,a]

yes

?- select(a, [x|X], [x,y,z]).
X = [a,y,z]

yes;
X = [y,a,z]

yes;
X = [y,z,a]

yes