I'm doing some exercises on prolog lists and I've already read this question, and seen the subtract/3 implementation. But I'm still a little bit confused.
Now I want to remove all occurrencies of an element from a given list, and after a few tries I have this:
% --- del all X occurrences from a list
del_all_X(_, [], _).
% unbinded T2
del_all_X(X, [X|T], T2):-
del_all_X(X, T, T2).
%!. ERROR: see below
del_all_X(Y, [X|T], [X|T2]):-
dif(Y, X), % USE this instead of !
del_all_X(Y, T, T2).
Which gives me this type of output:
25 ?- del_all_X(x, [x,a,b,x,d,e,x,x], X).
X = [a, b, d, e|_G4115].
- Which is the right way of thinking while binding free variables (like
T2_)? - How to have a clean output ?
Answer: del_all_X(_, [], []).
- Is there a simpler implementation without caling external functions ?
Thanks
EDIT : added corrections and answers in the question and in the code.