Updated April 15, 2023
Introduction to Prolog Reverse
Prolog reverse is a part of the list function to display values in the reverse format using a programming language. It is the method to display prolog list values in the opposite format. It shows list data from last to start positions order using methods in programming. The prolog revere is a display method to show the list data in reverse order like the queue function. It helps to display list value in the application as per the requirement to operate data using language. It is a method to show the value of the list using the append and accumulate method in the prolog programming language. It uses to sort the collection of the data and operates list values from bottom to top place.
Syntax of Prolog Reverse
The prolog reverse needs some method, objects, and values in the pl files.
The prolog reverse in pl file syntax is shown below.
value_reverse(A1, B1) :-
value_reverse(A1, [], B1, B1).
value_reverse([], B1, B1, []).
value_reverse([A|A1], C1, B1, [_|Bds]) :-
value_reverse(A1, [A|C1], B1, Bds).
Explanation:
- The value_reverse is a method to contain attributes.
- The “A”, “B”, and “C” are the attributes.
- These attributes are represented head, tail, and reversed object.
- This method works as a recursion. This reverse element works recursion using a loop.
- The B1 is the base case of the elements and [A|C1] shows the result using the object.
Within console syntax is shown below.
value_reverse([value1, value2, value3|A], Result_value).
In console syntax shown below.
value_reverse(Result_value, [value1, value2, value3|A]).
In console syntax shown below.
reverse(Result_value, [value1, value2, value3|A]).
Explanation:
- The value_reserve is a method included in the pl file.
- This method name might be the same in the pl file and prolog console.
- The prolog console can use the “reverse” keyword for the method.
- The value of the list can be numerical or character.
- The result set object uses before or after list value.
How does Reverse Work in Prolog?
Prolog comment works in the “pl” file.
Create a file with the “pl” extension.
Example: main.pl
Insert data into the prolog file.
Save the object with value into a pl file.
The following commands can use for the prolog reverse data of the list.
- Initialize the list data and reverse with objects.
value_reverse(A1, B1) :-
value_reverse(A1, [], B1, B1).
- Contains start and remain data object in the list.
value_reverse([], B1, B1, []).
- The head and tail value of the list inserts into the pl file.
- The result set object is placed inside of the method.
value_reverse([A|A1], C1, B1, [_|Bds]) :-
value_reverse(A1, [A|C1], B1, Bds).
Prolog comment works in the console.
- Open prolog console or interpreter.
- Set the directory path of the “pl” file.
- Use the given prolog file.
[main].
- You can use the prolog console directly for programming.
- Use the prolog reverse syntax in the prolog console.
| ?- value_reverse(Result_value, [1, 2, 3|A]).
- Use the prolog reverse syntax in the prolog console.
| ?- value_reverse( [1, 2, 3|A], Result_value).
- Use the with reverse keyword syntax in the prolog console.
| ?- reverse( [1, 2, 3|A], Result_value).
Combine the working procedure of the function.
Use the “main.pl” file.
value_reverse(A1, B1) :-
value_reverse(A1, [], B1, B1).
value_reverse([], B1, B1, []).
value_reverse([A|A1], C1, B1, [_|Bds]) :-
value_reverse(A1, [A|C1], B1, Bds).
Use the given prolog console with the required directory path.
[main].
| ?- value_reverse(Result_value, [1, 2, 3|A]).
Or
| ?- value_reverse( [a, b, c |A], Result_value).
Examples of Prolog Reverse
Given below are the examples mentioned:
Example #1
The basic prolog reverse example and the output show below.
The prolog (pl) file:
value_reverse(A1, B1) :-
value_reverse(A1, [], B1, B1).
value_reverse([], B1, B1, []).
value_reverse([A|A1], C1, B1, [_|Bds]) :-
value_reverse(A1, [A|C1], B1, Bds).
Code:
| ?- value_reverse( [a, b, c, d, e, f, g |A], Result_value).
Output:
Explanation:
- The data list uses the character values for operation.
- The function displays the list value in the opposite order.
- The list data contain character values with the head of the list.
- The result set object is used at the end of the list.
Example #2
The basic example and output shows below.
The prolog (pl) file:
value_reverse(A1, B1) :-
value_reverse(A1, [], B1, B1).
value_reverse([], B1, B1, []).
value_reverse([A|A1], C1, B1, [_|Bds]) :-
value_reverse(A1, [A|C1], B1, Bds).
Code:
| ?- value_reverse(Result_value, [a, b, c, d, e, f, g |A]).
Output:
Explanation:
- The data list uses the character values for operation.
- The function displays the list value in the opposite order.
- The list data contain character values with the head of the list.
- The result set object is used at the start of the list.
Example #3
The basic with reverse keyword example and the output shows below.
The prolog (pl) file:
value_reverse(A1, B1) :-
value_reverse(A1, [], B1, B1).
value_reverse([], B1, B1, []).
value_reverse([A|A1], C1, B1, [_|Bds]) :-
value_reverse(A1, [A|C1], B1, Bds).
Prolog console:
| ?- reverse( [a, b, c, d, e, f, g |A], Result_value).
Output:
Explanation:
- The function uses the “reverse” keyword.
- The function displays the list value in the opposite order.
- The list data contain character values with the head of the list.
- The result set object is used at the end of the list.
Example #4
The basic with numerical data list example and the output shows below.
The prolog (pl) file:
value_reverse(A1, B1) :-
value_reverse(A1, [], B1, B1).
value_reverse([], B1, B1, []).
value_reverse([A|A1], C1, B1, [_|Bds]) :-
value_reverse(A1, [A|C1], B1, Bds).
Prolog console:
| ?- reverse( [1, 2, 3, 4, 5, 6, 7 |A], Result_value).
Output:
Explanation:
- The function displays the list value in the opposite order.
- The list data contain character values with the head of the list.
- The data list uses the numerical value for prolog reverse operation.
Conclusion
The function operates list data user-friendly and easy. It helps to collect and manages list values in the application as per requirement. It helps to display and determines list data from start to end.
Recommended Articles
We hope that this EDUCBA information on “Prolog Reverse” was beneficial to you. You can view EDUCBA’s recommended articles for more information.