In this post, we will see how to reverse a list in prolog.
reverse(, Y, R) :- R = Y. reverse([H|T] , Y, R) :- reverse(T, [H|Y], R).
The first parameter in the reverse/3 predicate is the list. The second parameter is an empty list. The third parameter is the reverse list.
The reverse/3 recursively pushes the elements from the beginning of the first list to the front of the second list. This reverses the order of the elements.
Our base condition is reverse(, Y, R). At this point, we have pushed all elements from the input list to Y. We set R to Y and backtrack.