I have a predicate like this:
swap(List, Index1, Index2, New_List).
List = [3,8,1,6].
Index1 = 1.
Index2 = 3.
With the swap, New_List will be:
New_List = [3,6,1,8].
I'm trying to implement some code, but I don't understand why It gives me: False.
Could you help me to understand why my code is wrong, and how can I fix it?
Code:
swap([],_,_,[]).
swap(List, Index1, Index2, New_List) :-
swap(List, 0, Index1, Index2, New_List),
print(New_List).
swap([],_,_,_,[]).
swap(List, Index, Index1, Index2, [H|R]) :-
Index \== Index1,
nth0(Index, List, H, _Rest),
Index_New is Index+1,
swap(List, Index_New, Index1, Index2, R).
swap(List, Index, Index1, Index2, [H|R]) :-
Index == Index1,
nth0(Index2, List, H, _Rest),
Index_New is Index+1,
swap(List, Index_New, Index1, Index2, R).
swap(List, Index, Index1, Index2, [H|R]) :-
Index == Index2,
nth0(Index1, List, H, _Rest),
Index_New is Index+1,
swap(List, Index_New, Index1, Index2, R).
Thank you.