Combinations of multiple lists - Prolog

后端 未结 4 1813
闹比i
闹比i 2020-12-11 20:10

I need to find the combinations in a list of lists. For example, give the following list,

List = [[1, 2], [1, 2, 3]]

These should be the ou

4条回答
  •  -上瘾入骨i
    2020-12-11 21:12

    For completeness, here is the augmented version of my comment-version. Note nilmemberd_t/2 which is inspired by memberd_t/2.

    nilmemberd_t([], false).
    nilmemberd_t([X|Xs], T) :-
       if_(nil_t(X), T = true, nilmemberd_t(Xs, T)).
    
    nil_t([], true).
    nil_t([_|_], false).
    
    list_comb(List, []) :-
       nilmemberd_t(List, true).
    list_comb(List, Ess) :-
       bagof(Es, maplist(member,Es,List), Ess).
    

    Above version shows that "only" the first clause was missing in my comment response. Maybe even shorter with:

    nilmemberd([[]|_]).
    nilmemberd([[_|_]|Nils]) :-
       nilmemberd(Nils).
    

    This should work for Prologs without constraints. With constraints, bagof/3 would have to be reconsidered since copying constraints is an ill-defined terrain.

提交回复
热议问题