How to multiply all elements of two lists with each other in Prolog

后端 未结 5 1123
终归单人心
终归单人心 2021-01-22 00:12

I am thinking how to multiply all elements of two list with each other. Then I want to put all results in List3. For example,

List1 = [1,3,5].
List         


        
5条回答
  •  有刺的猬
    2021-01-22 00:44

    Use dcg! "Cross product"—shown here—is just one of many applications. Proceed like this:

    :- meta_predicate xproduct(4,?,?,?).
    xproduct(P_4,As) -->
       xproduct(P_4,As,As).
    
    :- meta_predicate xproduct(4,?,?,?,?).
    xproduct(P_4,As,Bs) -->
       xproduct_aux1(As,Bs,P_4).                % use 1st argument indexing for As
    
    :- meta_predicate xproduct_aux1(?,?,4,?,?).
    xproduct_aux1([]    ,_ , _ ) --> [].
    xproduct_aux1([A|As],Bs,P_4) -->
       xproduct_aux2(Bs,[A|As],P_4).            % use 1st argument indexing for Bs
    
    :- meta_predicate xproduct_aux2(?,?,4,?,?).
    xproduct_aux2([],_,_) --> [].
    xproduct_aux2([B|Bs],As,P_4) -->
       xproduct_(As,[B|Bs],P_4).
    
    :- meta_predicate xproduct_(?,?,4,?,?).
    xproduct_([],_,_) --> [].
    xproduct_([A|As],Bs,P_4) -->
        xprod_(Bs,A,P_4),
        xproduct_(As,Bs,P_4).
    
    :- meta_predicate xprod_(?,?,4,?,?).
    xprod_([],_,_) --> [].
    xprod_([B|Bs],A,P_4) -->
        call(P_4,A,B),
        xprod_(Bs,A,P_4).
    

    Let's use clpfd and lambdas to run the query you provided in your question:

    :- use_module([library(clpfd),library(lambda)]).
    
    ?- phrase(xproduct(\X^Y^[Z|Zs]^Zs^(Z #= X*Y),[1,3,5],[2,6,7]),Fs).
    Fs = [2,6,7,6,18,21,10,30,35].
    

    Above lambdas use difference-lists explicitly; with phrase//1 we can also use them implicitly!

    ?- phrase(xproduct(\X^Y^phrase(([Z],{Z #= X*Y})),[1,3,5],[2,6,7]),Fs).
    Fs = [2,6,7,6,18,21,10,30,35].
    

    clpfd enables us to do very general queries. Thanks to @PauloMoura for his suggestion! Look!

    ?- phrase(xproduct(\X^Y^phrase(([Z],{Z #= X*Y})),As,Bs),
              [2,6,7,6,18,21,10,30,35]),
       maplist(labeling([]),[As,Bs]).
      As = [-2,-6,-7,-6,-18,-21,-10,-30,-35], Bs = [-1]
    ; As = [ 2, 6, 7, 6, 18, 21, 10, 30, 35], Bs = [ 1]
    ; As = [-1,-3,-5],                        Bs = [-2,-6,-7]
    ; As = [ 1, 3, 5],                        Bs = [ 2, 6, 7]
    ; As = [-1],                              Bs = [-2,-6,-7,-6,-18,-21,-10,-30,-35]
    ; As = [ 1],                              Bs = [ 2, 6, 7, 6, 18, 21, 10, 30, 35]
    ; false.
    

提交回复
热议问题