I’d like to have a Prolog predicate that can replace the nth item in the list with the first.
Example:
% replace(+List,+Counter,-New List, %-First Item).
?- replace([1,2,3,4,5],3,L,Z).
L = [1, 2, 1, 4, 5]
Z = 1
This code replaces the nth item in the list with the last. I`m looking sth like this:
%+List,+Counter,-New List, %-Last Item
replace([X|Xs],I,[X|Xs1],Xz):-
I\=1,
I1 is I-1,
replace(Xs,I1,Xs1,Xz).
replace([_|Xs],1,[Xz|Xs1],Xz):-
replace(Xs,0,Xs1,Xz).
replace([Xz],_,[Xz],Xz).
I don’t know how to do this. Thanks for your help!