I understand the prologue, trying to implement the removal of the first and last element from the list. The first one is deleted, but I don’t understand how to change the code so that the last one is deleted.

domains i=integer list=i* predicates del(i,list,list) clauses del(1,[_|T],T):-!. del(N,[_,Y|T],L):-N1=N-1,del(N1,[Y|T],L). goal del(1,[2,5,6,-5,7,8,11,16,23,4],L). 
  • Something seems to me, this rule is not quite true. del (N, [_, Y | T], L): - N1 = N-1, del (N1, [Y | T], L). Along the way, he removes the first N elements (to be honest, now there is no place to check). If I didn’t really stick, then something like del (N, [A | T], [A | L]) should be: N1 = N-1, del (N1, T, L). - rasmisha
  • pancake, and one more question: how to delete the last two elements? - wicS

2 answers 2

If it is necessary to remove both the first and the last, it seems to me that this should work:

 dellast([_],[]):-! dellast([X|T],[X|Y]):-dellast(T,Y) delfirstandlast([_,T],L):-dellast(T,L). 
  • @rasmisha? thanks, works) - wicS

Removing only the first:

 delfirst([_|T],T). % И никакой рекурсии не нужно 

Delete the last:

 dellast([_],[]):-!. dellast([X|T],[X|Y]):-dellast(T,Y). 
  • @insolor, here it works separately ... and how to combine it, so that the first and last are removed immediately? somehow in the goal to write, I do not understand ( - wicS