I read Learn Prolog Now website and I try to do the following exercise:
6.5 Write a predicate swapfl(List1,List2) which checks whether List1 is identical to List2 , except that the first and last elements are exchanged. Here’s where append/3 could come in useful again, but it is also possible to write a recursive definition without appealing to append/3 (or any other) predicates.
I've written it and it works correctly, but it tries to find more than one solution.
swapfl([HL1|TL1],[HL2|TL2]):-
swpfl(TL1,TL2,HL2,HL1).
swpfl([HL1|TL1],[HL2|TL2],AccEL1,AccEL2):-
swpfl(TL1,TL2,AccEL1,AccEL2),
HL1=HL2.
swpfl([H1],[H2],H1,H2).
I look at the code and the trace
[trace] [5] ?- swapfl([1,2,3],[3,2,1]).
Call: (51) swapfl([1, 2, 3], [3, 2, 1]) ? creep
Call: (52) swpfl([2, 3], [2, 1], 3, 1) ? creep
Call: (53) swpfl([3], [1], 3, 1) ? creep
Exit: (53) swpfl([3], [1], 3, 1) ? creep
Call: (53) 2=2 ? creep
Exit: (53) 2=2 ? creep
Exit: (52) swpfl([2, 3], [2, 1], 3, 1) ? creep
Exit: (51) swapfl([1, 2, 3], [3, 2, 1]) ? creep
true ;
Redo: (52) swpfl([2, 3], [2, 1], 3, 1) ? creep
Fail: (52) swpfl([2, 3], [2, 1], 3, 1) ? creep
Fail: (51) swapfl([1, 2, 3], [3, 2, 1]) ? creep
false.
and I cannot understand why prolog thinks that it may be worth to start Redo part.
Could someone explain why there is an unsearched branch of the solution tree?