Create a prolog program to compute the union the intersectio

Create a prolog program to compute the union, the intersection, the difference of two sets. Use a few examples to verify your program. Submit your program and screenshots.

Solution

intrSets(_, [], []).
intrSets([], _, []).
intrSets([hy1|ty1], L2, [hy1|L]):-
member(hy1, L2),
intrSets(ty1, L2, L), !.
intrSets([_|ty1], L2, L):-
intrSets(ty1, L2, L).

intersection(L1, L2):-
intrSets(L1, L2, L),
write(L).


unionSet([], [], []).
unionSet([], [hy|ty], [hy|L]):-
intrSets(ty, L, Res),
Res = [],
unionSet([], ty, L),
!.
unionSet([], [_|ty], L):-
unionSet([], ty, L),
!.

unionSet([hy1|ty1], L2, L):-
intrSets([hy1], L, Res),
Res \\= [],
unionSet(ty1, L2, L).
unionSet([hy1|ty1], L2, [hy1|L]):-
unionSet(ty1, L2, L).

union(L1, L2):-
unionSet(L1, L2, L),
write(L).

Create a prolog program to compute the union, the intersection, the difference of two sets. Use a few examples to verify your program. Submit your program and s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site