3

これは問題のステートメントです:

整数のリスト L と整数 S が与えられた場合、要素の合計が S になるすべてのサブリストを生成します。

これが私の解決策です:

domains
        list=integer*
        clist=list*
predicates
        sum(list,integer)
        check(clist,clist,integer)
        gensub(list,list)
        getsub(list,clist,integer)
clauses
        %gets all possible subsets with gensub and findall, binds them to
        %and then checks the subsets to see if the elements add up to S
        %and adds the correct ones to Rez.
        %Goal example: getsub([1,2,5,6],X,7)
        getsub([], [], _).
        getsub(L, Rez, S):-
                findall(LR, gensub(L,LR), X),
                check(X, Rez, S).

        %generates all possible subsets of the given set
        gensub([], []).
        gensub([E|Tail], [E|NTail]):-
                gensub(Tail, NTail).
        gensub([_|Tail], NTail):-
                gensub(Tail, NTail).

        %checks if the sum of the elements of a list is S
        sum([], S):-S=0.
        sum([H|T], S):-
                N=S-H,
                sum(T, N).

        %checks if each sublist of a given list of lists, has the sum of elements S
        check([], [], S).
        %the LR variable here gives a warning after entering the goal
        check([H|T], [H|LR], S):-
                sum(H, S).

したがって、それを実行してゴールを求められた後getsub([1,2,5,6],X,7) 、要素の合計が7になるすべてのサブセットを取得しようとしましたが、チェック句No Solutionの変数に関する警告とLR、変数はバインドされていません。何が間違っているのか、またはこれに対するより簡単な解決策があるかどうかはわかりません。どんな助けでも大歓迎です。

4

2 に答える 2