Problem statement: You're given a list containing integers and lists of integers. You must remove from each sublist, the 1st, 2n, 4th, 8th.. etc, element.
My solution
domains
list=integer*
elem=i(integer);l(list)
clist=elem*
predicates
modify(list, list, integer, integer)
exec(clist, clist)
clauses
modify([], [], _, _).
modify([H|T], Mod, I, P):-
P=I,
!,
I1=I+1,
P1=P*2,
modify(T, Mod, I1, P1).
modify([H,T], [H|Mod], I, P):-
I1=I+1,
modify(T, Mod, I1, P).
exec([], []).
exec([i(N)|T], [i(N)|LR]):-
exec(T, LR).
exec([l(L)|T], [l(Mod)|LR]):-
modify(L, Mod, 1, 1).
do():-
exec([i(1),l([1,2,3,4,5,6,7,8,9]),l([1,2,3,4])],X),
write(X).
The problem is that the algorithm works until it removes the 1st and 2nd element from each sublist, but from then on doesn't remove a thing and I'm not sure what I'm doing wrong.
the exec predicate is used to assert whether the current element is an integer or a list of integers, add the integer to the result, or add the modified list to the result.
the modify predicate modifies a given list and should remove all elements on position power of 2.
I wrote the do predicate just for calling it as a goal, to avoid writing the list every time I want to test it.
I think you're doing
P1=P*2too often, then you mismatch successive powers of 2also, you have a typo here
should read
I would write
To keep is_pow2/1 simple, you could do
or use your Prolog arithmetic facilities. In SWI-Prolog a simple minded definition could be