% removes multiple occurrences of an element, % keeps only one occurrence of each element (the last one) % Example: %?- remove_duplicates([a,b,c,b,b,a],R). %R = [c, b, a] ; %No remove_duplicates([], []). remove_duplicates([H| T1], T2) :- member(H, T1),!, remove_duplicates(T1, T2). remove_duplicates([H| T1], [H| T2]) :- not(member(H, T1)), % can take this our becasue of CUT remove_duplicates(T1, T2). % Same predicate with accumulator, keeps first occurrence % Example: %?- r_duplicates([a,b,c,b,b,a],R). %R = [a, b, c] ; %No r_duplicates(L, R) :- r_helper(L, [], R). r_helper([],A,A). r_helper([H|T],A,R):- member(H,T),!,r_helper(T,A,R). r_helper([H|T],A,R):- r_helper(T,[H|A],R).