Creating lists do not include duplicates in Prolog -


**facts** player(milan,[seedorf,zambrotta,gattuso]). player(inter,[seedorf,ronaldo,zambrotta]). player(realmadrid,[seedorf,zidane,ronaldo]). 

i wanna create predicate such :

find (team, players) 

and if goal find(x,y) returns list of teams,x, , list of players,y, without duplicates... below:

x=[milan], y=[seedorf,zambrotta,gattuso]; x=[inter], y=[seedorf,ronaldo,zambrotta]; x=[realmadrid], y=[seedorf,zidane,ronaldo];  x=[milan,inter] y=[seedorf,zambrotta]; x=[milan,realmadri] y=[seedorf];  ... x=[milan,inter,realmadrid] y=[seedorf]; ...  

i try gives "error: out of local stack", if not use remove_dups predicate, list of teams,"x", have duplicates , program not stop... keep going x=[milan,milan,milan,milan,inter] .... how can correct code. ?:

find([x], y) :- player(x1, y),remove_dups(x1,x). find([x|xs], y) :- player(x1, y0),find(xs, y3), intersection(y0, y3, y),remove_dups(x1,x).  remove_dups([],[]). remove_dups([first|rest],newrest):-member(first, rest),remove_dups(rest, newrest). remove_dups([first|rest],[first|newrest]):-not(member(first, rest)),remove_dups(rest, newrest). 

thanks lot...

when pattern matching on list of xs, puts in same value of milan there lot of duplication. can avoid first ensuring no duplication in list of xs , finding corresponding players:

subset([], []). subset(xs, [_|ys]) :- subset(xs, ys). subset([x|xs], [x|ys]) :- subset(xs, ys).  allteams(ts) :- findall(t, player(t, _), ts). teams(t) :- allteams(ts), subset(t, ts).  find1([t], l) :- player(t, l). find1([t|ts], l) :- player(t, l0), find1(ts, l1), intersection(l0, l1, l).  find(x, y) :- teams(x), find1(x, y). 

here find set of teams first , try find subsets satisfying condition.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -