In Prolog how do you write a procedure that can be used to test whether or not a list represents a set with no duplicates?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are several ways to do this. @thanosQR is right in pointing to SWI-Prolog’s
is_set/1, but if you want a portable solution, you can define that predicate in terms ofsetof:A list contains no duplicates if its number of elements is equal to the number of elements in the
setofits elements.You can also use the (I believe non-standard, but commonly available)
sort/2, which eliminates duplicates:This takes O(n lg n) time to run.