
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Loop & Decision Making
- Conjunctions & Disjunctions
Prolog Operators
- Prolog - Type of Operators
- Prolog - Arithmetic Comparison Operators
- Prolog - Unification Operators
- Prolog - Term Comparision Operators
- Prolog - Arithmetic Operators
- Prolog - Logical Operators
- Prolog - List Operators
- Prolog - Custom Operators
Prolog Lists
- Prolog - Lists
- Prolog - Member of List
- Prolog - Length of List
- Prolog - Concatenating Lists
- Prolog - Appending to a List
- Prolog - Deleting from a List
- Prolog - Inserting into a List
- Prolog - Permutation Operation
- Prolog - Combination Operation
- Prolog - Reverse Items of a List
- Prolog - Shift Items of a List
- Prolog - Check Order of a List
- Prolog - SubSet of a Set
- Prolog - Union of Sets
- Prolog - Intersection of Sets
- Prolog - Even and Odd Length Finding
- Prolog - Divide a List
- Prolog - Find Maximum of a List
- Prolog - Find Minimum of a List
- Prolog - Find Sum of a List
- Prolog - Sorting List using MergeSort
Built-In Predicates
- Prolog - Built-In Predicates
- Prolog - Identifying Terms
- Prolog - Decomposing Structures
- Prolog - Collecting All
- Prolog - Mathematical Predicates
- Prolog - Scientific Predicates
Miscellaneous
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Preventing Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Practical Arithmetic Examples
- Prolog - Examples of Cuts
- Towers of Hanoi Problem
- Prolog - Linked Lists
- Monkey and Banana Problem
- Prolog Useful Resources
- Prolog - Quick Guide
- Prolog - Useful Resources
- Prolog - Discussion
Prolog - Reverse a List
Prolog provides an inbuilt predicate reverse to reverse a List.
Syntax
reverse(List, ReverseList)
Where
List − List to reverse.
ReverseList − The resulted reversed List.
Getting reverse of a List
GNU Prolog 1.5.0 (64 bits) Compiled Jul 8 2021, 12:33:56 with cl Copyright (C) 1999-2021 Daniel Diaz | ?- reverse([1, 2, 3], R). R = [3,2,1] yes
Checking a List as a valid reversed
| ?- reverse([1,2,3,4],[4,3,2,1]). yes
Reversing an empty List
| ?- reverse([],R). R = [] yes | ?-
Custom Reverse Operation
Suppose we have a list L = [a,b,c,d,e], and we want to reverse the elements, so the output will be [e,d,c,b,a]. To do this, we're creating a clause, list_reverse(List, ReversedList). Following are some observations −
If the list is empty, then the resultant list will also be empty.
Otherwise put the list items namely, [Head|Tail], and reverse the Tail items recursively, and concatenate with the Head.
Otherwise put the list items namely, [Head|Tail], and reverse the Tail items recursively, and concatenate with the Head.
Program (list_repos.pl)
list_concat([],L,L). list_concat([X1|L1],L2,[X1|L3]) :- list_concat(L1,L2,L3). list_rev([],[]). list_rev([Head|Tail],Reversed) :- list_rev(Tail, RevTail),list_concat(RevTail, [Head],Reversed).
Output
| ?- consult('D:/TP Prolog/Sample Codes/list_repos.pl'). compiling D:/TP Prolog/Sample Codes/list_repos.pl for byte code... D:/TP Prolog/Sample Codes/list_repos.pl compiled, 3 lines read - 1055 bytes written, 6 ms yes | ?- list_rev([a,b,c,d,e],NewList). NewList = [e,d,c,b,a]
Checking a List as a valid reversed
yes | ?- list_rev([a,b,c,d,e],[e,d,c,b,a]). yes
Checking an invalid List as a valid reversed list
| ?- list_rev([a,b,c,d,e],[a,d,c,b,e]). no | ?-
Explanation
list_concat([],L,L) represents the base case of the recursive call for concatenating lists. In case list is empty, the second non-empty List is returned.
-
list_concat([X1|L1],L2,[X1|L3]) :- list_concat(L1,L2,L3) represents the recursive case.
[X1|L1] is to divide the first list where X1 represents the head of the list and L1 represents the tail of the list.
L2 is the second List.
[X1|L3] is to get final concatenated List where X1 is appened to top of concatenated sublists represented by L3.
list_concat(L1,L2,L3) is the recursive call to List_concat with tail L1 and the second list L2 which is to return a concatenated List as L3.
list_rev([],[]) represents the base case of the recursive call for reversing the list. In case list is empty, the result will be an empty list
-
list_rev([Head|Tail],Reversed) :- list_rev(Tail, RevTail),list_concat(RevTail, [Head],Reversed). represents the recursive case.
[Head|Tail] is to divide the original list where Head represents the head of the list and Tail represents the tail of the list.
Reversed is the resulted reversed List.
list_rev(Tail, RevTail) is recursive call to reverse the tail list and storing the same in RevTail.
list_concat(RevTail, [Head],Reversed) is the recursive call to List_concat with tail RevTail and the second list having head which is to return a concatenated reversed List as Reversed.