简体   繁体   English

prolog 中列表列表的联合?

[英]Union of list of lists in prolog?

How is it possible to get the following (swi-)prolog predicate:如何获得以下(swi-)prolog谓词:

union(+ListOfLists, -ResultList)

in the form:形式:

ListOfLists = [A1, A2, A3, ..., An]

ResultList = A1 ∪ A2 ∪ A3 ∪ ... ∪ An 

If your "union" is commutative and if the identity element is the empty set, you could trivially define:如果您的“联合”是可交换的并且标识元素是空集,您可以简单地定义:

union(Ls, U, Op, Empty) :-
    foldl(Op, Ls, Empty, U).

How do you represent your union?你如何代表你的工会? If it is a list, then [] is the empty set, and if you already have a union/3 defined (where the last argument is the union of the first two), then:如果它是一个列表,那么[]是空集,并且如果您已经定义了一个union/3 (其中最后一个参数是前两个的并集),那么:

union(Ls, U) :-
    foldl(union, Ls, [], U).

But what is the computational complexity of your union/3 ?但是你的union/3的计算复杂度是多少?

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

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