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
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...
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.