Reputation: 1
I want to write a predicate that will be taking every element from my list and mult with others without duplicates.
Examples:
?- predicate([2,3,5,6],X).
X = [6,10,12,15,18,30]. % expected result
?- predicate([1,6,10],X).
X = [6,10,60]. % expected result
I was trying something like this, but I don't know how to upgrade this code:
predicate([],[]).
predicate([_|[]],[]) :-
!.
predicate([H,S|T],[V|X]) :-
V is H*S,
predicate([H|T],X).
Upvotes: 0
Views: 311
Reputation: 3407
The following gives the results you describe:
predicate(Xs, Ys):-
aggregate_all(
set(Y),
(
member(X1, Xs),
member(X2, Xs),
X1 =\= X2,
Y is X1 * X2
),
Ys
).
Notice that aggregate_all/3
is a non-standard predicate from library aggregate
. There are alternative implementations that use the ISO predicate bagof/3
.
Upvotes: 1