I tried to understand how this code works, but I failed. It merges the two lists and then cancels the result.
reverse(L, RL):- reverse(L, [], RL).
reverse([], RL, RL).
reverse([H|T], S, RL):- reverse(T, [H|S], RL).
concat([], L, L).
concat([H|T1], L2, [H|T]):- concat(T1, L2, T).
concat_reverse(L1,L2,L):-concat(L1,L2,LN),reverse(LN,L)
My teacher told me to go into debug mode and trace the request to understand, but it did not help.
here is an example
5 ?- trace,concat_reverse([1,3],[4,5],S).
Call: (7) concat_reverse([1, 3], [4, 5], _G1444) ? creep
Call: (8) concat([1, 3], [4, 5], _G1569) ? creep
Call: (9) concat([3], [4, 5], _G1564) ? creep
Call: (10) concat([], [4, 5], _G1567) ? creep
Exit: (10) concat([], [4, 5], [4, 5]) ? creep
Exit: (9) concat([3], [4, 5], [3, 4, 5]) ? creep
Exit: (8) concat([1, 3], [4, 5], [1, 3, 4, 5]) ? creep
Call: (8) reverse([1, 3, 4, 5], _G1444) ? creep
Call: (9) reverse([1, 3, 4, 5], [], _G1444) ? creep
Call: (10) reverse([3, 4, 5], [1], _G1444) ? creep
Call: (11) reverse([4, 5], [3, 1], _G1444) ? creep
Call: (12) reverse([5], [4, 3, 1], _G1444) ? creep
Call: (13) reverse([], [5, 4, 3, 1], _G1444) ? creep
Exit: (13) reverse([], [5, 4, 3, 1], [5, 4, 3, 1]) ? creep
Exit: (12) reverse([5], [4, 3, 1], [5, 4, 3, 1]) ? creep
Exit: (11) reverse([4, 5], [3, 1], [5, 4, 3, 1]) ? creep
Exit: (10) reverse([3, 4, 5], [1], [5, 4, 3, 1]) ? creep
Exit: (9) reverse([1, 3, 4, 5], [], [5, 4, 3, 1]) ? creep
Exit: (8) reverse([1, 3, 4, 5], [5, 4, 3, 1]) ? creep
Exit: (7) concat_reverse([1, 3], [4, 5], [5, 4, 3, 1]) ? creep
S = [5, 4, 3, 1].