Prolog Directions: Implement a set as lists, where each element of a set appears
ID: 3558142 • Letter: P
Question
Prolog
Directions: Implement a set as lists, where each element of a set appears exactly once in its lsit, but in no particular order. Do not assume you can sort the list. Do assume that input lists have no duplicate elements, and do guarantee that output lists have no duplicate elements.
Define the isIntersection predicate so that isIntersection(X,Y,Z) says that the intersection of X and Y is Z. Do not use the predefined list predicates. Your predicate may choose a fixed order for Z. Your predicate need not work well when X or Y are unbound variables.
All implementation should be done in the programming language Prolog
Explanation / Answer
intersectionTR(_, [], []). intersectionTR([], _, []). intersectionTR([H1|T1], L2, [H1|L]):- member(H1, L2), intersectionTR(T1, L2, L), !. intersectionTR([_|T1], L2, L):- intersectionTR(T1, L2, L). intersection(X, Y):- intersectionTR(X, Y, Z), write(Z).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.