gnu Prolog powerset 修改

发布于 2024-10-01 18:09:27 字数 423 浏览 5 评论 0原文

所以我得到了这个 powerset:

powerset([], []).
powerset([H|T], P) :- powerset(T,P).
powerset([H|T], [H|P]) :- powerset(T,P).

这会生成列表的所有集合。是否可以按列表顺序生成所有集合。

示例:

List = [a,b,c]

我想得到

[a],[a,b],[a,b,c],[b],[b,c],[c]

注意,此子集列表中没有 [a,c] 因为这些子集是从左侧开始向右的。

我尝试过使用追加和递归的组合,但这并没有达到我想要的效果。此时小小就愣住了。

谢谢。

So i got this for powerset:

powerset([], []).
powerset([H|T], P) :- powerset(T,P).
powerset([H|T], [H|P]) :- powerset(T,P).

This generates all sets of a list. Is it possible to generate all sets in list order.

Example:

List = [a,b,c]

I want to get

[a],[a,b],[a,b,c],[b],[b,c],[c]

Note there is no [a,c] in this list of subsets since these are subsets starting from the left and going to the right.

I've tried using a combination of append and recursion, but that didn't work out as i wanted it to. Little stumped at this point.

Thanks.

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

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

发布评论

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

评论(4

热风软妹 2024-10-08 18:09:27

怎么样

powerset(L, [H|T]):-
  append([H|T], _, L).
powerset([_|L], P):-
  powerset(L, P).

How about

powerset(L, [H|T]):-
  append([H|T], _, L).
powerset([_|L], P):-
  powerset(L, P).
弱骨蛰伏 2024-10-08 18:09:27

我知道这是一篇旧文章,但我不会无缘无故地更新它。
得到所有尊重的答案,单独生成所有子集,并且不生成幂集。

几天前,我试图实现一个 powerSet/2 谓词,而不使用内置谓词 bagof/2。但即使使用 bagof/2 和 setof/2 对于初学者来说这也不是一个很容易的问题(单独生成所有子集是另一个问题,而且要容易得多)。因此,在实现该解决方案之后,我认为最好将其放在这里,以防止正在搜索该主题的人出错。

我的解决方案(没有 bagof/2

generate(X, [Y], [[X| Y]]).

generate(X, S, P) :-
        S = [H| T],
        append([X], H, Temp),
        generate(X, T, Rest),
        append([Temp], Rest, P), !.

powerSet([], [[]]).

powerSet(Set, P) :-
        Set = [H| T],
        powerSet(T, PsT),
        generate(H, PsT, Ps),
%       write('trying to push '), print(H), write(' to '),
%       write('all elements of powerset of '), print(T), write(' which is '),
%       print(PsT), nl,
%       write('and the result is '), print(Ps), nl, nl,
        append(Ps, PsT, P), !.

如果参考注释行,

代码将会被理解。 此处提供了另一种解决方案,它使用内置谓词bagof/3

也许现在会更有帮助。

I know this an old post, but I'm not gonna update it for no reasons.
the answer which is accepted with all the respect, generates all subsets separably, and does not generate a powerset.

a few days ago I was trying to implement a powerSet/2 predicate, without use of built-in predicate bagof/2. but even with bagof/2 and setof/2 it's not very easy problem for beginners (generating all subset separably is another problem and much easier). so after implementing the solution I thought it's better to put it here in order to prevent people who are searching for this topic from mistakes.

My solution (without bagof/2)

generate(X, [Y], [[X| Y]]).

generate(X, S, P) :-
        S = [H| T],
        append([X], H, Temp),
        generate(X, T, Rest),
        append([Temp], Rest, P), !.

powerSet([], [[]]).

powerSet(Set, P) :-
        Set = [H| T],
        powerSet(T, PsT),
        generate(H, PsT, Ps),
%       write('trying to push '), print(H), write(' to '),
%       write('all elements of powerset of '), print(T), write(' which is '),
%       print(PsT), nl,
%       write('and the result is '), print(Ps), nl, nl,
        append(Ps, PsT, P), !.

code will be understood if consulted with the commented lines.

another solution is available here which uses built-in predicate bagof/3.

probably it would be more helpful now.

沙与沫 2024-10-08 18:09:27

您需要所有子序列子字符串(定义)。语法(DCG)最适合这个:


seq([]) -->
   [].
seq([E|Es]) -->
   [E],
   seq(Es).

... --> [] | [_], ... .

?- Es = [_|_], phrase((..., seq(Es), ...), [A,B,C]).
   Es = [A]
;  Es = [A, B]
;  Es = [A, B, C]
;  Es = [B]
;  Es = [B, C]
;  Es = [C]
;  false.

You want all subsequences substrings (definition). Grammars (DCGs) are best for this:


seq([]) -->
   [].
seq([E|Es]) -->
   [E],
   seq(Es).

... --> [] | [_], ... .

?- Es = [_|_], phrase((..., seq(Es), ...), [A,B,C]).
   Es = [A]
;  Es = [A, B]
;  Es = [A, B, C]
;  Es = [B]
;  Es = [B, C]
;  Es = [C]
;  false.
情徒 2024-10-08 18:09:27

这个怎么样:

powerset(A,B):-
    append(A,_,B);
    append(_,A,B).

test():-
    setof(X,powerset(X,[1,2,3]),L),
    writeln(L).

How about this:

powerset(A,B):-
    append(A,_,B);
    append(_,A,B).

test():-
    setof(X,powerset(X,[1,2,3]),L),
    writeln(L).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文