Prologue

I need help with the routine I'm trying to create. I need to make a routine that will look something like this:

difference([(a,b),(a,c),(b,c),(d,e)],[(a,_)],X).

X = [(b,c),(d,e)].

I really need help with this ...

I have written a method so far that can remove the first occurrence that it finds. However, I need to remove it from all occurrences. Here is what I still have ...

memberOf(A, [A|_]).
memberOf(A, [_|B]) :-
    memberOf(A, B).

mapdiff([], _, []) :- !.
mapdiff([A|C], B, D) :-
        memberOf(A, B), !,
        mapdiff(C, B, D).
mapdiff([A|B], C, [A|D]) :-
        mapdiff(B, C, D).

I took this code from the list (subtract).

I do not quite understand what he is doing, but I know that is almost what I want. I did not use subtraction, because my last code should be compatible with WIN-Prolog ... I am testing it on SWI Prolog.

+3
source share
3 answers

Difficult! Modest coffee has the right idea. Here's a fancy solution using double negation:

difference([], _, []).
difference([E|Es], DL, Res) :-
    \+ \+ member(E, DL), !,
    difference(Es, DL, Res).
difference([E|Es], DL, [E|Res]) :-
    difference(Es, DL, Res).

SWI-PROLOG. :

1: . !

2: E DL , member/2 true, , member/2 , , , , (a,_) . , \+ , member/2, \+ true, . , 3- , .

3: .

+3

, - . findall, , :

?- findall(X, (member(X, [(a,b),(b,c),(a,c)]), X \= (a,_)), Res).

Res = [ (b, c) ]

,

removeAll(Pattern, List, Result) :-
  findall(ZZ109, (member(ZZ109, List), ZZ109 \= Pattern), Result).

, , ZZ109 Pattern ( , , . WIN-Prolog ), difference :

difference(List, [], List).
difference(List, [Pattern|Patterns], Result) :- 
  removeAll(Pattern, List, Result1),
  difference(Result1, Patterns, Result).
+2

, , memberOF , , , , . SWI Prolog :

memberOf(A, [B|_]) :- unifiable(A,B,_).

WIN-PRolog, , , , .

+2

Source: https://habr.com/ru/post/1725187/


All Articles