Combinations of multiple lists - Prolog

后端 未结 4 1816
闹比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条回答
  •  执念已碎
    2020-12-11 21:05

    A twist in @false's approach:

    %list_comb( ++LL, -Ess)
    list_comb( LL, Ess):-
        is_list( LL),
        maplist( is_list, LL),
        findall( Es, maplist( member, Es, LL), Ess).
    

    Testing:

    41 ?- list_comb( [[1,2],[1],[1]], X).
    X = [[1, 1, 1], [2, 1, 1]].
    
    42 ?- list_comb( [[1,2],[1],[1,2,3]], X).
    X = [[1, 1, 1], [1, 1, 2], [1, 1, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3]].
    
    43 ?- list_comb( [[1,2],[],[1,2,3]], X).
    X = [].
    
    44 ?- list_comb( [[1,2],t,[1,2,3]], X).
    false.
    
    45 ?- list_comb( t, X).
    false.
    

提交回复
热议问题