简体   繁体   English

Prolog交集列表

[英]Prolog intersection list of lists

I want to create an intersection of lists of lists in prolog. 我想在序言中创建列表的交集。 (Matrix, with lists as cells) (矩阵,列表作为单元格)

I have to handle only the case, when number of rows and columns are the same (Rectangular). 当行数和列数相同时(矩形),我只需要处理这种情况。 The lists are ordered, and does not contain any duplicate elements (they are ord_sets). 列表是有序的,并且不包含任何重复的元素(它们是ord_sets)。

How could I do that? 我该怎么办?

Example: (3 rows, 3 columns) 示例:(3行3列)

A:
[[[1,2],[3,2,1],[3,4,5]],
[[1,2],[3,2,1],[3,4,5]],
[[1,2],[3,2,1],[3,4,5]]]
B:
[[[1],[3,2,1],[3,4,5]],
[[1,2],[2,1],[3,4]],
[[1,2],[3,2,1],[3,9,10,4,5]]]
C:
[[[1],[3,2,1],[3,4,5]],
[[1,2],[2,1],[3,4]],
[[1,2],[3,2,1],[3,4,5]]]

Thank you for the help! 感谢您的帮助!

Most Prolog interpreters already have a predicate to calculate the intersection between two lists: intersection/3 . 大多数Prolog解释器已经有一个谓词可以计算两个列表之间的交集: intersection/3 For example: 例如:

?- intersection([3,2,1], [3,9,10,4,5], R).
R = [3].

We can use maplist/3 to process an entire row of such lists: 我们可以使用maplist/3处理此类列表的整行:

?- maplist(intersection, [[1,2],[3,2,1],[3,4,5]], [[1],[3,2,1],[3,4,5]], C).
C = [[1], [3, 2, 1], [3, 4, 5]].

And by using another maplist/3 we process the matrices: 通过使用另一个maplist/3我们可以处理矩阵:

?- maplist(maplist(intersection),[[[1,2],[3,2,1],[3,4,5]], [[1,2],[3,2,1],[3,4,5]], [[1,2],[3,2,1],[3,4,5]]], [[[1],[3,2,1],[3,4,5]],[[1,2],[2,1],[3,4]],[[1,2],[3,2,1],[3,9,10,4,5]]], C).
C = [[[1], [3, 2, 1], [3, 4, 5]], [[1, 2], [2, 1], [3, 4]], [[1, 2], [3, 2, 1], [3, 4, 5]]].

So we can do the processing with: 因此,我们可以使用以下方法进行处理:

intersect_matrix(A, B, C) :-
    maplist(maplist(intersection), A, B, C).

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

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