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!

List Manipulation Question

Status
Not open for further replies.

Univarn

Programmer
Mar 30, 2008
1
US
Hey, I've been working with prolog and am coming along at a so-so speed but I have a question. Is there any way once I have a list that I'm using to track movements to take things out of that list from another list comparison? Pretty much I want to do a set difference but I want to retain the original list in terms of variable.

So lets say V = [v1,b2,v2,b3,v5,b5] and I want to remove all instances of the b# combination. So B = [b1,b2,b3,b4,b5] lets say. After I do this though I'd like to hold V as the list but with only v values.

Hope that makes sense...
 
Solution:

% When down to one if A is member of M the result
% is empty list
function([A],M,[]):-
member(A,M).
% else A is in the result
function([A],M,[A]):-
\+member(A,M).
% when more than one, make function of tail and simply
% return L1 because H is member of M
function([H|T],M,L1):-
function(T,M,L1),
member(H,M).
% if H is not member of M, insert it into the result of
% function of tail ...
function([H|T],M,L2):-
function(T,M,L1),
\+member(H,M),
insert(H,L1,L2).

% function insert which uses delete :)
delete(X,[X|T],T).
delete(X,[Y|T1],[Y|T2]):-
delete(X,T1,T2).
insert(X,L,L1):-
delete(X,L1,L).

Best regrads!
 
And you can also add:

function([],_,[]).

So that empty list returns empty list ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top