I have code in PROLOG:
vals(_,[],_).
vals([H|T],[(H,C)|L],K) :- vals([H|T],L,(K,C)).
This code receivers list and list of tuples, for example:
vals([1],[(1,4),(1,2)],X).
I check if element from first list is equal to some tuples first element from the other list. In this case foundValues will return true, because 1 is equal to each tuples first element. This works fine, but instead of returning true/false, in resulting list I want to return all second element of each tuple where its first element is equal to the element from list. In this case X should be [4,2]. I am trying to do this with (K,C), but no success. So, the question is - How to return list?
vals/3
doesn't really do anything. You could make it be the list you want. If the head of[H|T]
matches the first element of the head of[(H,C)|L]
what do you think the head of the 3rd argument should be? And where do you think the tail of the 3rd argument should come from (or how should it be determined)? – lurker[C|R]
. You just have to tell it how to get the tail of the result (R
) from the tails of the inputs. It's easier than you think. – lurker