# How to delete the first occurence of EACH element of list?

I know how to remove the first occurence of an element.
For example, we have a list: [a,a,a,b,b,b,c,c,c,d,d,d].

``````del(_, [], []).
del(X, [X|T], T).
del(X, [H|T], [H|R]) :- del(X, T, R).

?- del(a, [a,a,a,b,b,b,c,c,c,d,d,d], X)
``````

From this code I will get the result [a,a,b,b,b,c,c,c,d,d,d].
But how should I write the code to get [a,a,b,b,c,c,d,d] in result?

Try

``````?- L = [a,a,a,b,b,b,c,c,c,d,d,d],L=[Head|Tail].
``````

When you try this it will show

``````L = [a, a, a, b, b, b, c, c, c|...],
Tail = [a, a, b, b, b, c, c, c, d|...].
``````

so you can do it this way

``````?- L = [a,a,a,b,b,b,c,c,c,d,d,d],L=[Head|Tail];true.
L = [a, a, a, b, b, b, c, c, c|...],
Tail = [a, a, b, b, b, c, c, c, d|...]
``````

Then press the `w` key to show

``````L = [a, a, a, b, b, b, c, c, c, d, d, d],
Tail = [a, a, b, b, b, c, c, c, d, d, d] .
``````

What should happen if you have the list `[a, b, c]`? How about `[a, b, b, a, b, b]`? etc.

Maybe you should start by outlining the algorithm in detail.

If I have the list [a,b,c], I will get an empty list. If [a, b, b, a, b, b] → [b, a, b, b].

As an other example: [a, b, a, c, d, c, a, b, f, d, b, a, b].

• The very first element is “a”: [a, b, a, c, d, c, a, b, f, d, b, a, b]. We haven’t deleted any “a” element from this list. So, we should do it.

• Now our list is: [b, a, c, d, c, a, b, f, d, b, a, b] and the first element is “b”: [b, a, c, d, c, a, b, f, d, b, a, b]. We also haven’t deleted any “b” element - so, we need to remove “b”.

• Now our list is: [a, c, d, c, a, b, f, d, b, a, b]. The first element is “a”, but we’ve deleted an “a” element. So, we take “c” element and we haven’t deleted any “c” elements => we remove “c” from the list: [a, c, d, c, a, b, f, d, b, a, b] → [a, d, c, d, b, f, d, b, a, b].

• [a, d, c, d, b, f, d, b, a, b] → [a, c, d, b, f, d, b, a, b] : We haven’t deleted any “d” - we delete the first “d” in the list.

• [ a, c, d, b, f, d, b, a, b] → [a, c, d, b, d, b, a, b] :We haven’t deleted any “f” - we delete the first “f” in the list.

• [a, c, d, b, d, b, a, b]: We’ve deleted “a”, “b”, “c”, “d” elements and we don’t have any other elements in the list

And the result is: [a, c, d, b, d, b, a, b]

So you would need some book-keeping where you collect the elements you see for the first time as you traverse the list?

I think yes, because otherwise program will “think” each element as seen for the first time

Can anybody help me? I tried to do book-keeping, but I don’t know how.
I also thought about other ways to resolve this problem. Maybe I need smth like this?

``````del1(_,[],[]).
del1(El,[El|T],T1):-del1(El,T,T1).
del1(El,[X|T],[X|T1]):-not(El=X),del1(El,T,T1).
del([],L,L).
del([H|Tail],L,Ans):-del1(H,L,Temp),del(Tail,Temp,Ans).
``````
``````?- del([a,b,c,d,f],  [a,b,a,c,d,c,a,b,f,d,b,a,b], X)
``````

It removes elements that are in the first list(there I write which “types” of elements we have) from the second list. But it removes ALL elements and I don’t know how to change it to my purpose

Is this homework? What are the limitations to what you can and cannot use? Can you use member/2? How about memberchk/2? How about \+/1?

Yes, this is homework, but about limitations is nothing said. I can use all this functions

You have all the building blocks then. Maybe start by writing in pseudocode the algorithm. Something like:

``````Input L: list,
Let Seen be empty list,
Let Result be empty list,
for each element E in L:
if E not in Seen then... else...
``````

and so on

You made a mistake here, you mutated an “a” into a “d”.