How to remove all lists from a list?

I would like to remove from the list of lists all lists containing [['One', _], _]. I created the following sentence delete_all, which always works, except in my case:

delete_all(_,[],[]) :- !.
delete_all(X,[X|Tail],List) :-
    !,
    delete_all(X,Tail,List).
delete_all(X,[A|Tail],[A|List]) :-
    delete_all(X,Tail,List).

Example: (this works well)

?- delete_all(3,[3,4,3,5,3],K).
K = [4, 5] .

but this works poorly:

delete_all([['One', _], _], [[['One', 'Six'], 94],
    [['One', 'Ten'], 13], [['Two', 'Nine'], 35]], Y).
Y = [[['One', 'Ten'], 13], [['Two', 'Nine'], 35]].

Why does it delete only the first element? What could be wrong?

+4
source share
2 answers

Good to check, here is a working one:

delete_all(E, L, R) :- findall(X, (member(X, L), X \= E), R).

I think your code does not work because it creates an instance of X and then blocks further matches. The documentation for delete / 3 (which does what you need by simply changing the arguments) speaks quite clearly about the problem.

You must change your first sentence.

delete_all(X,[E|Tail],List):-
    \+ X \= E, !, delete_all(X,Tail,List).
+1

, , (, ) unify ' . , , , , .

, unifiable, .

- :

delete_all(_,[],[]):-!.

delete_all(X,[H|Tail],List):-
    unifiable(X, H, _),
    !,
    delete_all(X,Tail,List).

delete_all(X,[A|Tail],[A|List]):-
    delete_all(X,Tail,List).

Yap, ().

:- use_module(library(terms)).

not(not(X = H)) unifiable, , H X, , true.

UPDATE: CapelliC \+ X \= E not(not(X = E)), .

+1

All Articles