Question

In Prolog, define the isUnion predicate so that isUnion(X,Y,Z) says that the union of X and...

In Prolog, define the isUnion predicate so that isUnion(X,Y,Z) says that the union of X and Y is Z. Do not use the predefined list predicates. Your predicate may choose a fixed order for Z. If you query isUnion([1,2],[3],Z) it should find a binding for Z, but it need not succeed on both isUnion([1],[2],[1,2]) and isUnion([1],[2],[2,1]). Your predicate need not work well when X or Y are unbound variables.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

isUnion predicate:
isUnion([Head|Tail],Y,Z) :- isMember(Head,Y), isUnion(Tail,Y,Z).
isUnion([Head|Tail],Y,[X|Z]) :- not(isMember(Head,Y)), isUnion(Tail,Y,Z).
isUnion([],Y,Y).

isMember predicate:
isMember(X, [X|_]).
isMember(X, [_|Tail]) :- isMember(X,Tail).

Add a comment
Know the answer?
Add Answer to:
In Prolog, define the isUnion predicate so that isUnion(X,Y,Z) says that the union of X and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT