Skip to content

Cutting cut from set difference #2507

Closed Answered by UWN
jjtolton asked this question in Q&A
Aug 27, 2024 · 5 comments · 4 replies
Discussion options

You must be logged in to vote

This was the original version.

deleteall([], _X, []).
deleteall([E|Es], X, Ys0) :-
   if_( E = X, Ys0 = Ys, Ys0 = [E|Ys] ),
   deleteall(Es, X, Ys).

?- deleteall([A,B,C,A,C],A,Es).
   A = B, C = A, Es = []
;  A = B, Es = [C,C], dif:dif(C,A)
;  A = C, Es = [B], dif:dif(B,A)
;  Es = [B,C,C], dif:dif(B,A), dif:dif(C,A).

Which only deletes one element X. But we want to delete all elements of Xs. So we have to replace the condition (=)/3 above by a more sophisticated condition.

deletealls([], _Xs, []).
deletealls([E|Es], Xs, Ys0) :-
   if_( memberd_t(E, Xs), Ys0 = Ys, Ys0 = [E|Ys] ),
   deletealls(Es, Xs, Ys).

?- deletealls([A,B,C,A,C],[A],Es).
   A = B, C = A, Es = []
;  A = B, Es = [C,C],…

Replies: 5 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@jjtolton
Comment options

@jjtolton
Comment options

Comment options

You must be logged in to vote
1 reply
@jjtolton
Comment options

Answer selected by jjtolton
Comment options

You must be logged in to vote
1 reply
@jjtolton
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants