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

I am thinking how to multiply all the elements from two lists with each other. Then I want to put all the results in List3

. For example,

List1 = [1,3,5].
List2 = [2,6,7]. 

      

List3

must contain [1x2, 1x6, 1x7, 3x2, 3x6, 3x7, 5x2, 5x6, 5x7]. Finally;

List3 = [2, 6, 7, 6, 18, 21, 10, 30, 35].

      

Can this be done? How to do it? I couldn't find the right way.

+2


source to share


5 answers


Ok first take a look at this question performing an operation on each list item in swi-prolog and others to see how to perform an operation for-each

on lists

.
Second, here's the code:

prod(X,[],[]).
prod(X,[HEAD|TAIL],L) :-  prod(X,TAIL,L1), W is X * HEAD, L = [W|L1].

prod2([],Y,[]).
prod2([HEAD|TAIL],Y,L) :- prod(HEAD,Y,L1), prod2(TAIL,Y,L2), append(L1,L2,L).

      



output:

?- prod2([1,3,5] ,[2,6,7],G).
G = [2, 6, 7, 6, 18, 21, 10, 30, 35] .

      

+1


source


Here is a pretty straight forward solution using library(lambda)

product(Xs, Ys, Ps) :-
   maplist(Ys+\X^maplist({X,Ys}+\Y^YP^(YP=X*Y),Ys), Xs, PPs),
   append(PPs, Ps).

      

So, we have an outer loop for Xs

and an inner loop for Ys

.



?- product([1,2,3],[4,5,6],Ps).
Ps = [1*4,1*5,1*6,2*4,2*5,2*6,3*4,3*5,3*6].

      

Replace (YP=X*Y)

with (YP is X*Y)

or (YP #= X*Y)

. Whatever you choose.

+3


source


Why not

prod(L1, L2, LP) :-
    bagof(P, X^Y^(member(X, L1), member(Y, L2), P is X * Y), LP).

      

+3


source


Use ! "Cross product" - here - is just one of many applications . Follow these steps:

:- 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 and lambdas before execute the request you mentioned 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 ; with the help 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].

allows us to make very general inquiries. Thanks @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.

      

+2


source


Simple solution, not requiring any Prolog extensions (but of course losing the potential benefits of using CLP (FD)):

product(List1, List2, Product) :-
    % save a copy of the second list
    product(List1, List2, List2, Product).

product([], _, _, []).
product([X| Xs], List2, Rest2, Product) :-
    (   Rest2 == [] ->
        product(Xs, List2, List2, Product)
    ;   Rest2 = [Y| Ys],
        Z is X * Y,
        Product = [Z| Zs],
        product([X| Xs], List2, Ys, Zs)
    ).

      

This solution is tail-recursive and leaves no false choice points.

+2


source







All Articles