I'm trying to solve a new program with Prolog, and I'm stuck, and don't know how to continue... I must do a predicate that has 3 arguments, the first is a list of elements, the second one is a list of tuples, and the third one must be a list returned that contains the second element of the tuples, if the first element of the tuple match with an element of the first argument list. It must delete copies also!!
For example,
check([a,c],[(a,aa),(bb,bbb),(a,aa),(c,def)],X).
X = [aa, def] .
As you can see, a and c match on the list of tuples, so return the second element of the tuples.
So it works, BUT if there is more than one tuple that contains a first element that match on the first list, it only will take once, for example:
check([a,b],[(a,c),(a,d),(b,c),(b,e),(c,f)],X).
X = [c] .
It finds a the first time and took c, and b the first time and took c again, but not iterate to find more a or b, the right result should be X=[c,d,e].
So please, I ask you help on how to solve this situation or any clue to solve it...
Here is my code:
check([],_,[]).
check(L,DIC,Xord) :- inter(L,DIC,X), list_to_set(X,Xord).
inter([],_,[]).
inter(L1, DIC, L3) :-
L1 = [H|T],
DIC = [(DIC1,DIC2)|_],
H == DIC1,
L3 = [DIC2|L3T],
inter(T, DIC, L3T).
inter(L1,[_|DIC],L3) :- inter(L1,DIC,L3).
inter([_|T], DIC, L3) :- inter(T, DIC, L3).
Thanks in advance for your time.