1
votes

I googled this but cant find the answer, so here you go:

I have this function in prolog:

ing(Lis) :- findall(I,( recipe2(_,ingredients(I,_)) ),Lis).

This function search and returns me a list of lists like this:

L = [['wheat flour', egg, salt], ['wheat flour', cheese, olives, tomato, salt, basil], ['wheat flour', potatoes, salt], [milk, egg, sugar]].

I want to unify that list of lists in only one list, so i can get out duplicates. I know i have to use recursion, but thats all i know.

Thanks in advance.

1

1 Answers

0
votes

You may simply modify the predicate like such:

ing(Lis) :-
    setof(E, X^Y^I^( recipe2(X, ingredients(I,Y)), member(E, I) ), Lis).

member/2 is a built-in predicate that unifies the first argument with an element of a list in the second argument. It is non-deterministic.

The use of X^Y^I^ are existential quantifiers to ensure that you only get your results in one solution. It essentially says,

There exists an X, Y, and I for any element E that is a part of an ingredient list, (I).

Using setof/3 also ensures that any solution you get will be a collection of unique elements.


member/2setof/3