Reputation: 19
the following code is not possible, because it can only remove the first two or last two.
delements(L,L1):- append([_,_],L1,L),append(L1,[_,_],L).
Upvotes: 1
Views: 88
Reputation: 476669
You should not reuse L1
and L
in the same list. You need an extra variable here:
delelements(L, R) :-
append(M, [_, _], L),
append([_, _], R, M).
So here M
is a list that contains the elements of L
, except for the last two. R
is a variant of M
, except that the first elements are removed.
This then give us:
?- delelements([1,4,1,3,0,2,2,5], R).
R = [1, 3, 0, 2] ;
false.
Furthermore we do not need to use append/3
[swi-doc] to remove a fixed number of elements from the head. We can use unification for this:
delelements(L, R) :-
append([_, _ | R], [_, _], L).
Upvotes: 3