0
votes

I'm having some problems in prolog... again I need to make a function that receives three lists: elementsToRemove fullList nonRepeatedElements The function should be this as follows:

removeRepeatedElements(elementsToRemove, fullList, nonRepeatedElements)

where nonRepeatedElements is a list without any element that is in elementsToRemve AND fullList. Can anyone please help! Kind of desperate over here. ahah

2
you want to get the union of unique elements ? i.e: func([1,2,3],[3,4,5],L). L = [1,2,4,5] ?a.u.r
no, func([1,2,3,4], [2,3], L). L=[1, 4]FriedRike

2 Answers

1
votes

SWI-Prolog has subtract(+Set, +Delete, -Result).

It's implemented in this way:

%%  subtract(+Set, +Delete, -Result) is det.
%
%   Delete all elements from `Set' that   occur  in `Delete' (a set)
%   and unify the  result  with  `Result'.   Deletion  is  based  on
%   unification using memberchk/2. The complexity is |Delete|*|Set|.
%
%   @see ord_subtract/3.

subtract([], _, []) :- !.
subtract([E|T], D, R) :-
    memberchk(E, D), !,
    subtract(T, D, R).
subtract([H|T], D, [H|R]) :-
    subtract(T, D, R).

You can use that implementation in any other Prolog...

0
votes

ok , according to your description , this would be the answer :

% base case
sto([],L,[]).

% item isn't found in the other list
sto([H|T],L,[H|T2]):-
\+member(H,L),sto(T,L,T2).

% item is present in the other list
sto([H|T],L,N):-
member(H,L),sto(T,L,N).

member(X,[X|_]).
member(X,[_|T]):-
member(X,T).

so , sto is the main function , member function checks if the element is present in the given list