This is an extended problem from this page. Prolog possible removal of elements in a list
For example, for a list X = [1,2,3], I can subtract like following:
subtract 1 from 1st / 2nd element, and X becomes [1-1, 2-1, 3] = [0, 1, 3] = [1, 3]
subtract 1 from 1st / 3rd element: X becomes [2, 2]
subtract 1 from 2nd / 3rd element: X becomes [1, 1, 2]
subtract 2 from 2nd / 3rd element: X becomes[1, 1]
So, it is always subtracting 2 elements, and with the same number, but always a real number. Have anyone got any ideas on this?
This looks better:
subt_comb(X, Y).
X = [1,2,3]
Y = [1,3]
Y = [2,2]
Y = [1,1,2]
Y = [1,1]
After having a look on the solutions of lurker and gusbro, I have created something like this.
remove2([HX|T], S2):-
between(1, HX, Y),
remove2__([HX|T], Y, S2).
remove2([HX|T], [HX|TY]):-
remove2(T, TY).
% remove2__(S1, Y, S2), this procedure is to execute 0 after
% subtraction. Y is generated from remove2(S1, S2).
remove2__([Y|T], Y, TY):- remove2_(Y, T, Y, TY).
remove2__([HX|T], Y, [HY|TY]):-
HX>Y,
HY is HX - Y,
remove2_(HX, T, Y, TY).
% remove2_(HX, L, Y, S2).
% HX is the first element from the origin list. L is the tail of the
% origin list. Y is the number to subtract. S2 is the result.
remove2_(_, [H|T], H, T).
remove2_(_, [H|T], Y, [HY|T]):- %for list with descending order
HY is H - Y, HY >0.
remove2_(HX, [H|T], Y, [H|TY]):- %going for another element.
remove2_(HX, T, Y, TY).
?- remove2([3,2,1],X).
X = [2, 1, 1] ;
X = [2, 2] ;
X = [1, 1] ;
X = [3, 1] ;
false.
?- remove2([1,2,3],X).
X = [1, 3] ;
X = [2, 2] ;
X = [1, 1, 2] ;
X = [1, 1] ;
false.