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!

Merge lists

Status
Not open for further replies.

jimisharris

Programmer
Jan 10, 2008
1
GR
hi,i want your help please,
I have the following facts in prolog and i want to write a function that merge the following facts in one list
l(ar, [c,d,f,g]).
l(br, [s,h,g,d]).
l(cr, [m,n,c]).
l(dr, [v,h,n,m]).
after that i want to know which of these elements in new list are exists more than one time.
thanks

 
this solve the first part of the problem. Put the facts in one list ...


lsl(ar, [c,d,f,g]).
lsl(br, [s,h,g,d]).
lsl(cr, [m,n,c]).
lsl(dr, [v,h,n,m]).

%List of facts in new list.
lstl(F):- findall(L,lsl(_,L),C),flatten(C,F).

 
%ok this solve all you want. just type oneTime(X).

lsl(ar, [c,d,f,g]).
lsl(br, [s,h,g,d]).
lsl(cr, [m,n,c]).
lsl(dr, [v,h,n,m]).

%List of facts in new list.
lstl(F):- findall(L,lsl(_,L),C),flatten(C,F).

one(X):-lsl(_,L),member(X,L).

%count the number of elements
ocorre(_,[],0).
ocorre(X,[X|R],N):- ocorre(X,R,N1), N is N1 +1,!.
ocorre(X,[_|R],N):- ocorre(X,R,N).

count([E,N]):-one(E),lstl(L),ocorre(E,L,N).

%Elements that exists more than one time
oneTime(C):-setof([E,N],(count([E,N]),\+ member(1,[E,N])),C).

 
I use this version of prolog, for windows

SWI-Prolog version 5.4.7 by Jan Wielemaker (jan@swi-prolog.org)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top