开发者

gnu Prolog powerset modification

开发者 https://www.devze.com 2023-01-24 15:31 出处:网络
So i got this for powerset: powerset([], []). powerset([H|T], P) :- powerset(T,P). powerset([H|T], [H|P]) :- powerset(T,P).

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. L开发者_运维知识库ittle stumped at this point.

Thanks.


How about

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


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.


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.


How about this:

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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号