I'm just starting to learn how to work with lists in prolog and I tried to make a program on the example of similar tasks, but it does not work. The task is - Remove from the first list items that have an even number of occurrences in the second list.
I have such logic in this functions:
- Function
countershould return a Quantity of some element at the list. - Function
removeignore of elements which has even quantity at the second list.
My code:
counter(_,[],0).
counter(Number, [H|T], Quantity):-
Number =:= H,
UpdatedQuantity is Quantity + 1,
counter(Number, T, UpdatedQuantity).
counter(Number, [H|T], Quantity):-
Number =\= H,
counter(Number, T, Quantity).
remove([], [], []).
remove([H1|T1], T2, [H1 | T1]) :-
counter(H1, T2, Quantity),
Quantity mod 2 =:= 0,
remove(T1, T2, T1).
remove([H1|T1], T2, T1) :-
counter(H1, T2, Quantity),
Quantity mod 2 =\= 0,
remove(T1, T2, T1).
For example, remove([1,2,3,4][1,1,2,3,3,3,4,4,4,4,5,6,7,7], Res) will return Res = [2,3,4,5,7,7]