Reputation: 59
i have this working predicate for difference of 2 sets
difference([],Y,[]).
difference([X|R],Y,Z) :-
member(X,Y),
!,
difference(R,Y,Z).
difference([X|R],Y,[X|Z]) :-
difference(R,Y,Z).
But i would like to make similar thing for 3 sets and i can't figure it out
difference(S1,S2,S3,result)
resulting in (S1\S2)\S3.
Upvotes: 2
Views: 288
Reputation: 476594
Well you basically already say it yourself: take the formula (S1\S2)\S3, substitute S1\S2 by T1 (a temporary), then the resulting formula is T1\S3.
So now put this into code:
difference(S1,S2,S3,Result) :-
difference(S1,S2,T1), %T1 = S1\S2
difference(T1,S3,Result). %Result = T1\S3=(S1\S2)\S3
Upvotes: 1