Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog remove multiple elements from a list

Tags:

list

prolog

I know how to remove an element from a list but is there a way to remove more than one elements from a list? For example,

deletelist([a,b,c,a,b],[a,c],X)
X = [b,b] % delete a and c from the list.
like image 885
MaXX99 Avatar asked May 23 '11 08:05

MaXX99


People also ask

How do I remove an item from a list in Prolog?

This predicate can be used to select an element from a list, delete an element or insert it. The definition of this Prolog library predicate is: delete(A, [A|B], B). delete(A, [B, C|D], [B|E]) :- delete(A, [C|D], E).


1 Answers

SWI-Prolog offers subtract/3:

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

?- listing(subtract).
lists:subtract([], _, []) :- !.
lists:subtract([A|C], B, D) :-
    memberchk(A, B), !,
    subtract(C, B, D).
lists:subtract([A|B], C, [A|D]) :-
    subtract(B, C, D).
like image 55
Kaarel Avatar answered Sep 26 '22 00:09

Kaarel