one of my predicates gives me a list like this as output:
[m(1,2,[v(1,y),v(1,y)]),m(1,6,[v(1,y),v(5,x)]),m(1,4,[v(3,x),v(1,y)]),m(1,8,[v(3,x),v(5,x)])]
When the symbols inside the sublist with v elements are equal (like y here):
[v(1,y),v(1,y)]
or x here:
[v(3,x),v(5,x)]
I have to sum the numbers on the left and rebuild the list like this:
[m(1,2,[v(2,y)]),m(1,6,[v(1,y),v(5,x)]),m(1,4,[v(3,x),v(1,y)]),m(1,8,[v(8,x)])]
I have a similar predicate that works on the main lists but I am not getting this one right.
This is the other predicate that is working:
simplify([], []) :- !.
simplify([X], [X]) :- !.
simplify([m(C, TD, Var), m(C2, TD, Var)| Xs], Ys) :-
sumxp(C, C2, K), simplify([m(K, TD, Var)|Xs], Ys), !.
simplify([X, Y|Xs], [X|Ys]) :- !, simplify([Y|Xs], Ys).
sumxp(Power1, Power2, TotalPower) :- TotalPower is Power1 + Power2.
It acts in this way:
simplify([m(2,2,[v(1,a)]),m(2,2,[v(1,a)])],R).
R = [m(4, 2, [v(1, a)])].
[v(3,x),v(5,x)]converted to[v(8,x)]is there another rule, or is the example wrong? - Guy Coder