-1

I want to define a relation del_all(X, L, L1) for removing all items X (if any) from list L.

Sample query:

?- del_all(a, [a,b,a,c,d,a], L1).
L1 = [b, c, d].                           % expected answer
repeat
  • 18,496
  • 4
  • 54
  • 166
  • You want to delete all *equal* items or all *unifiable* items? Or is your list elements *always* ground, making the distinction mute? – Paulo Moura May 26 '16 at 13:52

4 Answers4

2

Here is another version that combines (=)/2 and dif/2:

list_without([], _, []).
list_without([X|Xs], H, Ys0) :-
   if_(X = H, Ys0 = Ys, Ys0 = [H|Ys]),
   list_without(Xs, H, Ys).

It uses if_/3

false
  • 10,264
  • 13
  • 101
  • 209
1
dele_all(X, [], []).
dele_all(X, [H|T], R) :-
   H = X,
   dele_all(X, T, R).
dele_all(X, [H|T], [H|R]) :-
   dif(H, X),
   dele_all(X, T, R).
repeat
  • 18,496
  • 4
  • 54
  • 166
1

Partitioning list items according to some given criterion is a common idiom in Prolog.

Using tfilter/3 in combination with dif/3 we can simply query:

?- tfilter(dif(a), [a,b,a,c,d,a], L1).
L1 = [b,c,d].                            % expected result
Community
  • 1
  • 1
repeat
  • 18,496
  • 4
  • 54
  • 166
0
del_all(_,[],[]).
del_all(Item,[Item|Rest],RRest) :-
    !,
    del_all(Item,Rest,RRest).
del_all(Item,[OtherItem|Rest],[OtherItem|RRest]) :-
    del_all(Item,Rest,RRest).

Simple as that.

Zebollo
  • 1
  • 1