Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing one lists contents from another...

Status
Not open for further replies.

Cetra

Programmer
Apr 22, 2009
2
US
I've looked through the forum but this problem seems unique. I'm trying to remove the contents of one list from another. For instance:

removed([1,2,3],[2],X).
should give the result X = [1,3]?

So far the code I have come up with is as follows:
removed(L,[],L).
removed([H|T],[H|S],T) :- removed(T,S,T).

This works only for the first element in the list, and I think I need another remove definition to traverse through elements whose heads are not the same, but that part I can't figure out.
 
I got a little bit further in this program. My source code is as follows:

remove(L,[],L).
remove([H|T],[H|S],Z) :- remove(T,S,Z).
remove([R|T],[Q|S],[R|Z]) :- remove(T,[Q|S],Z).

This works in a few cases such as:
-? remove([1,2,3,1],[1,1],X).

giving the result of X = [2,3]

However the result I'm looking for is to be able to take out multiple like terms in one list, if they are listed in the other. Anyone have any input?
 
I think it should be like that
Code:
% remove(L1, L2, LR)
% L1 list to work
% L2 list of elements to remove
% LR list result

% when L2 if empty, it's done
remove(L, [], L).

% you work element by element
remove(L, [H | T], LR) :-
  remove_one(L, H, L1),
  remove(L1, T, LR).
You just have to write remove_one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top