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
A simple solution not requiring any Prolog extensions (but, of course, loosing the potential benefits of using CLP(FD)) would be:
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 doesn't leave spurious choice-points.