This is completely not relevant to all my posts and a work I do. I am taking a “Prolog” class and find this language just amazing. So decided to post some of my assignments, because probably students all over a world also get similar one
My assignment is to write a program that removes duplicate list members, BUT you cannot use “member” predicate.
So if my list is empty there are no duplicate members for sure
The same is true if I have only one member in the list
Now comes the tricky part. If we recognize 2 members in the list with a same value, we will return the same list , but only with one member
And this is the start point for the program. Get the list and check it , by sending list tail to recursion
Ok, lets run it….
Well, we definitely got a correct answer, but what about incorrect answers? This is because of Prolog backtracking mechanism. After it find the correct answer it doesn’t stop and continue to check other options. So we add “red cut” to prevent backtracking
Now we have the correct answer