New in Prolog, trying to write a predicate to provide all the options that an element can be inserted into the list at any position. Example:
ins ins(a, [b,c], R).should indicate:
R = [a,b,c]
R = [b,a,c]
R = [b,c,a]
which he does, but then gives the error "Out of Global stack". Is there a way to make it more deterministic, give results and do it? When it starts in the opposite direction, i.e. ins (X, Y, [a, b, c]). It gives the expected results, and then says false, indicating that it has completed. The code:
app([],L,L).
app([H|T],L2,[H|L]) :-
app(T,L2,L).
ins(E, List, R) :-
R = R1,
app(R2, R3, R1),
app([E], R4, R3),
app(R2, R4, List).
Here is the link to run the code in the online compiler, SWISH (This is also an example of how I hope to use ins, but ins are a problem right now) Any help would be appreciated!