% Write a Prolog program, remove-nth(Before, After) that asserts the After list
% is the Before list with the removal of every n’th item from every list at all
% levels.
/*delete a number in the list. */
delte(1,[_|T],T).
delte(P,[X|Y],[X|R]):-
P1 is P-1,
delte(P1,Y,R).
/* delete before and after. */
daltob(P,L,R):-
P1 is P-1,
delte(P1,L,R1),
/* delete before. */
delte(P,R1,R).
/* delete after. */
% Output
|
Prolog Program to delete before and after of the nth element in the list. |
% The above Prolog program was not working for boundary conditions when P is 1
% or when P is n(size of the list). Some changes are made in the code to make
% it work for boundary conditions as well.
% :::::::::::Code after above mentioned changes::::::::::::
% Write a Prolog program, remove-nth(Before, After) that asserts the After list
% is the Before list with the removal of every n’th item from every list at all
% levels.
/*delete a number in the list. */
delte(1,[_|T],T).
delte(P,[X|Y],[X|R]):-
P1 is P-1,
delte(P1,Y,R).
/* delete before and after. */
% Its also working for first and last positions.
% Used nested if else conditions.
daltob(P,L,R):-
length(L,L1),
(
P=:=1 ->
P3 is P+1,
delte(P3,L,R)
;
P=:=L1 ->
P3 is P-1,
delte(P3,L,R)
;
P1 is P-1,
delte(P1,L,R1),
/* delete before. */
delte(P,R1,R)
/* delete after. */
).
% Output
|
Prolog Program to delete before and after of the nth element in the list. |
No comments:
Post a Comment