Updated April 5, 2023
Introduction to Prolog Reverse List
Prolog reverse list is defined as the reversing of the given list, it means this operation is used to find reverse of a given list by using prolog programming language, prolog is a logical and declarative programming language, suppose there is a list L=[a,b,c,d,e,f ], and want to reverse the elements of list, so the output will be [f,e,d,c,b,a], to perform above operation need to create a clause that is ‘list_reverse(List, ReversedList)’, if the list is empty then the output/resultant list will also be empty, and also we can put the names as [Head|Tail], reversing of tail items recursively and concatenate with the head.
Syntax of Prolog Reverse List
The syntax for reverse list is:
list_reverse(List, ReversedList).
Where, the List is the input list and ReversedList is the output list.
- List: The list is the data structure that is used in different cases for non-numeric programming, lists are used to store a number of items, and the list can either be empty or non-empty, the list is written as in [ ], list has two-part one part is called as a head and remaining part of the list is called a tail.
Suppose,L= [a,b,c] is the list ,if we take [b,c]=Tail then the list will be L=[a|Tail], head and tail separated by vertical bar(|). - ReversedList: This is the reverse list we will get after performing reverse operation on list, if the list is empty then the resultant list will also be empty, we can give input through the syntax.
?- reverse([a,b,c,d], Result).
And the output will be in the below format:
Result = [d,c,b,a]
There are some other syntaxes:
reverse_worker(Xs,[],Ys).
The worker used in this input source is used when the accumulator is seeded as an empty list.
reverse_worker([] ,R,R).
This type of input source will be used when the list is empty and the accumulator contains the reversed list.
How to Reverse List in Prolog?
- To illustrate the reversing of a list, we will take the example of defining a predicate that has a list ([a,b,c,d]) as input and returns a list containing the same elements in the reverse order that is ([d,c,b,a]). Now a reverse predicate is a powerful predicate to have around. The lists in prolog are easy to access from the front and from the back also.
- For example, to pull out the head of a list L, we can perform unification [H|_]=L, this result in H is being proceeded to the head of L, pulling off the last element of an order is not easy, so that we cannot do it by using simple unification. If we had a predicate that reverses lists, then we could first reverse the input list and we will have to pull out the head of the reversed list then we would get the last element of the list. So a ‘reverse’ predicate is a useful tool, we can reverse large lists by using a ‘reverse’ predicate which is more efficient.
- We have two types of reverse predicates one is naive, which is defined with the help of ‘append’ and the other is by using accumulators which is more efficient and more natural. ‘Append’ is a useful predicate but it is also important to us that we cannot use it all time because it can be a source of inefficiency. It is inefficient because it does not join two lists in one simple action, this is the weak point for ‘append’, rather it needs to work its way down its first argument until it finds the end of the list, and only then it carries out concatenation. This cause no problem, let’s take an example that if we have two lists and we just want to concatenate them, for that prolog will need to work down the length of the first list but if the list is not too long then it is easy to work with append, but in the list have two given variables then first the list get splitting and then start working.
- So that the reversing by using ‘Accumulator’ is a better way to reverse lists, it means accumulator is itself a list, and when we start it will be empty. Suppose we want to reverse [a,b,c,d], at first accumulator is empty [ ], so we simply take the head of the lists that we trying to reverse, and then we will add it as the head of the accumulator. Then we proceed to reverse the tail then our task is to reverse [b,c,d] as we take [a] as an accumulator after that again we take the head of the list and will add it as the head of the accumulator and now [b,a] is the new accumulator we proceed to reverse [c,d]. By repeating this we get a new accumulator [c,b,a] then reverse [d], then at the next step we get accumulator as[d,c,b,a], in this way we get the reverse list.
Examples of Prolog Reverse List
Given below are the examples mentioned:
Example #1
Code:
:- initialization(main).
main :- write('Hello World!').
list_reverse([],[]).
list_reverse([_First|_Rest],_Reversed):-
list_reverse(rest,_ReversedRest).
concatenation(_ReversedRest,[_First],_Reversed).
concatenation([],L,L).
concatenation([X1|L1],L2,[X1|L3]):-concatenation(L1,L2,L3).
Input:
reverse([a,b,c,d],Results).
Output:
In the above example, we reverse the list as shown in output by putting input as a list[a,b,c,d], and by performing reverse operation we get a reverse list as shown in the output figure. In the program at first, we take and then concatenate them by using L1,L2, L3 we get the result.
Example #2
Code:
:- initialization(main).
main :- write('Hello World!').
% reverseList([1,2,3], ReversedList).
reverseList([H|T], ReversedList):-
reverseListHelper(T,[H], ReversedList).
reverseListHelper([], Acc, Acc).
reverseListHelper([H|T], Acc, ReversedList):-
reverseListHelper(T, [H|Acc], ReversedList).
Input:
reverseList([1,2,3], Ls).
Output:
This is another example by taking accumulator, in which we use accumulator and done reversing by its syntax and then by putting input we get the output.
Conclusion
In the above article, we conclude that Prolog contains its own library of list programs, in terms of reversing a list all the arguments are reversed from the first and then it returns true or false, as we have seen that prolog has a reverse function with its syntax has argument head and tail.
Recommended Articles
We hope that this EDUCBA information on “Prolog Reverse List” was beneficial to you. You can view EDUCBA’s recommended articles for more information.