I need to program a Predicate in Prolog that inserts an element in the indicated position and consequently modifies the position number of the rest of the elements in the list. What I've achieved is the next snippet of code that implements a Predicate which inserts an element at the end of the list. In the database sector, apart from LIST we've got also LONG that indicates the amount of elements in the list. At the end there's some code on my try to implement de predicate. Could anyone tell me what's wrong in there? I'm lost in here.
Domains
name=symbol
position=integer
element=integer
Database
list(name,position,element)
long(name,integer)
Predicates
nondeterm inserirf(element)
Clauses
list(b,1,1).
list(b,2,5).
list(b,3,8).
list(b,4,3).
long(b,4).
inserirf(V):-
long(b,X),
Y=X+1,
assertz(list(b,Y,V)),
assertz(long(b,Y)),
retract(long(b,X)),
long(b,Q),
list(b,Q,P),
write(P),nl.
Goal
inserirf(7).
My last try:
Predicates
nondeterm inserirl(nom,pos,element)
Clauses
list(b,1,1).
list(b,2,5).
list(b,3,8).
list(b,4,3).
list(b,5,10).
list(b,6,11).
long(b,6).
inserirl(L,Pos,E):-
long(L,Long),
Pos > Long,
NouLong = Long+1,
retract( long(L,Long) ),
assertz( list(L,Pos,NouLong) ),
assertz( long(L,NouLong) ).
inserirl(L,Pos,E):-
long(L,X),
XaPassar=X-1,
retract(llista(L,Pos,E)),
retract( long(L,X) ),
assertz( long(L,XaPassar) ),
inserirl(L, XaPassar,E),
long(L,Y),
Y2=Y+1,
retract( long(L,Y) ),
assertz( long(L,Y2) ),
assertz(llista(L,Pos,E)).
Goal
inserirl(b,3,9).
% 3 -> position
% 9 -> element
% b -> name of list
Hundreds thanks to any help.