简体   繁体   English

统一列表列表prolog

[英]Unifying list of lists prolog

I googled this but cant find the answer, so here you go: 我用谷歌搜索了,但找不到答案,所以你走了:

I have this function in prolog: 我在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. 提前致谢。

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. member/2是一个内置谓词,它将第一个参数与第二个参数中列表的元素统一起来。 It is non-deterministic. 这是不确定的。

The use of X^Y^I^ are existential quantifiers to ensure that you only get your results in one solution. X^Y^I^是存在量词,以确保您只在一个解决方案中获得结果。 It essentially says, 它基本上说,

There exists an X, Y, and I for any element E that is a part of an ingredient list, (I). 对于作为成分列表的一部分的任何元素E,存在X,Y和I,(I)。

Using setof/3 also ensures that any solution you get will be a collection of unique elements. 使用setof/3还可以确保您获得的任何解决方案都是唯一元素的集合。


Documentation (SWI-Prolog) for member/2 and setof/3 成员/ 2setof / 3的文档(SWI-Prolog)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM