Question: SWI Prolog - Remove Duplicates from The list (Explain this solution) The solution is as follows. Numbers denote line numbers and are unrelated to prolog

SWI Prolog - Remove Duplicates from The list (Explain this solution)

The solution is as follows.

Numbers denote line numbers and are unrelated to prolog

1: compress([],[]). 2: compress([X],[X]). 3: compress([X,X|Xs],Zs) :- compress([X|Xs],Zs). 4: compress([X,Y|Ys],[X|Zs]) :- X \= Y, compress([Y|Ys],Zs).

I'm having a difficult time tracing this solution and how it works.

Line 1 evaluates to true when list is empty Line 2 evaluates to true when both lists contain the same elements Line 3 (Here's where I get confused) Does a recursive call, I think it strips the first element from the list? Line 4 ??? I have little idea what's going on here. If X is not equal to Y, then do some recursion call.

I think the biggest part I'm confused about is the recursion calls using a pipe operator like line 3 and 4. This usually signifies a head and tail, why are we passing it here?

Thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!