Prolog findall 存在量词

发布于 2024-09-15 10:13:38 字数 1321 浏览 5 评论 0原文

我在 Prolog 中遇到了一个问题。 这是我使用的一些代码。

has_same_elements([X|_],Y) :-
    permutation(X,Xnew),        
    member(Xnew,Y), !.
has_same_elements([_|Tail],Y) :-
    has_same_elements(Tail,Y).

这获取两个列表列表作为输入,并确定它们是否包含具有相同元素的列表。例如,[[1,2],[3,4]][[2,1],[4,3]] 具有相同的元素。这很好用。

现在我的 findall:

findall(V, (verdeling2(S,Perm,V), \+X^(X\=V,verdeling2(S,Perm,X),has_same_elements(X,V))) ,Verd).

重要的是要知道 verdeling2/3 是一个返回不同列表列表的子句(如上所述),并且它是由 [1,2,3,4,...] 的排列构成的 verdeling2/3 的一些不同输出(根据作为输入的排列)是:

V = [[[1, 2], [3, 4]]] ;
V = [[[2, 1], [3, 4]]] ;
V = [[[2, 3], [1, 4]]] ;
V = [[[2, 3], [4, 1]]] ;
V = [[[1, 3], [2, 4]]] ;
V = [[[3, 1], [2, 4]]] ;
V = [[[3, 2], [1, 4]]] ;
V = [[[3, 2], [4, 1]]] ;
V = [[[1, 3], [4, 2]]] ;
V = [[[3, 1], [4, 2]]] ;
V = [[[3, 4], [1, 2]]] ;
V = [[[3, 4], [2, 1]]] ;
V = [[[1, 2], [4, 3]]] ;
V = [[[2, 1], [4, 3]]] ;
V = [[[2, 4], [1, 3]]] ;
V = [[[2, 4], [3, 1]]] ;
V = [[[1, 4], [2, 3]]] ;
V = [[[4, 1], [2, 3]]] ;
V = [[[4, 2], [1, 3]]] ;
V = [[[4, 2], [3, 1]]] ;
V = [[[1, 4], [3, 2]]] ;
V = [[[4, 1], [3, 2]]] ;
V = [[[4, 3], [1, 2]]] ;
V = [[[4, 3], [2, 1]]] ;

现在我想要一些东西,它可以让我概述所有不包含相同元素的列表(使用 has_same_elements)。我认为我使用 findall 应该可以解决问题,但它返回完整的数据包,而不是过滤我不想要的数据包。

I have a problem with a problem in Prolog.
Here's some of the code I use.

has_same_elements([X|_],Y) :-
    permutation(X,Xnew),        
    member(Xnew,Y), !.
has_same_elements([_|Tail],Y) :-
    has_same_elements(Tail,Y).

This gets two lists of lists as input, and decides whether or not they contain lists with the same elements. E.g. [[1,2],[3,4]] has the same elements as [[2,1],[4,3]]. This works fine.

Now my findall:

findall(V, (verdeling2(S,Perm,V), \+X^(X\=V,verdeling2(S,Perm,X),has_same_elements(X,V))) ,Verd).

All that's important to know is that verdeling2/3 is a clause that returns different lists of lists (as mentioned above), and it's constructed from a permutation of [1,2,3,4,...]
Some different outputs of verdeling2/3 (according to the permutation as input) are:

V = [[[1, 2], [3, 4]]] ;
V = [[[2, 1], [3, 4]]] ;
V = [[[2, 3], [1, 4]]] ;
V = [[[2, 3], [4, 1]]] ;
V = [[[1, 3], [2, 4]]] ;
V = [[[3, 1], [2, 4]]] ;
V = [[[3, 2], [1, 4]]] ;
V = [[[3, 2], [4, 1]]] ;
V = [[[1, 3], [4, 2]]] ;
V = [[[3, 1], [4, 2]]] ;
V = [[[3, 4], [1, 2]]] ;
V = [[[3, 4], [2, 1]]] ;
V = [[[1, 2], [4, 3]]] ;
V = [[[2, 1], [4, 3]]] ;
V = [[[2, 4], [1, 3]]] ;
V = [[[2, 4], [3, 1]]] ;
V = [[[1, 4], [2, 3]]] ;
V = [[[4, 1], [2, 3]]] ;
V = [[[4, 2], [1, 3]]] ;
V = [[[4, 2], [3, 1]]] ;
V = [[[1, 4], [3, 2]]] ;
V = [[[4, 1], [3, 2]]] ;
V = [[[4, 3], [1, 2]]] ;
V = [[[4, 3], [2, 1]]] ;

Now I'd want something that gives me an overview of all lists that don't contain the same elements (using has_same_elements). I thought my use of findall should do the trick, but it returns full packet, instead of filtering the ones I don't want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2024-09-22 10:13:38

我并不假设您使用某种约束逻辑编程语言
或类似的东西,以便 A\=B 的作用不仅仅是 \+ A=B

我猜 X\=V 总是失败,因此它背后的目标没有被执行,
并且否定 \+ 始终为真。

X\=V 总是失败,因为 X=V 总是成功,因为 X 是新的
您的上下文中的变量。

也许一些重新排序会有所帮助。

此致

I am not assuming that you use some constraint logic programming language
or somesuch so that A\=B does more than only \+ A=B.

I guess the X\=V always fails so that the goals behind it are not executed,
and the negation \+ is always true.

The X\=V always fails, since X=V always succeeds, since X is a fresh
variable in your context.

Probably some reordering would help.

Best Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文