
- 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 - Shifting Items of a List
Shifting operation in List refers to moving the first element to the end of the list and vice versa often termed as left rotation or right rotation. As in Prolog, List is a immutable data structure, shifting creates a new List with elements arranged as per the shift operation.
Left Shift Operation
Using Left Shift operation, we can shift one element of a list to the left rotationally. So if the list items are [a,b,c,d], then after left shifting, it will be [b,c,d,a]. So we will make a clause left_shift(L1, L2).
We will express the list as [Head|Tail], then recursively concatenate Head after the Tail, so as a result we can feel that the elements are shifted.
This can also be used to check whether the two lists are shifted at one position or not.
Program (list_repos.pl)
left_shift([], []). left_shift([Head|Tail],Shifted) :- append(Tail, [Head],Shifted).
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, 0 lines read - 436 bytes written, 6 ms (15 ms) yes | ?- left_shift([a,b,c,d,e],L2). L2 = [b,c,d,e,a] yes | ?- left_shift([a,b,c,d,e],[b,c,d,e,a]). yes | ?-
Explanation
left_shift([], []) is base case where shifting empty list results in empty list.
left_shift([Head|Tail],Shifted) :- list_concat(Tail, [Head],Shifted). shift the list in rotational manner.
[Head|Tail] is to divide the original list where Head represents the head of the list and Tail represents the tail of the list.
Shifted is the resulted shifted List.
append(Tail, [Head],Shifted) call appends the Tail with the List having head as only element which is to return a concatenated shifted List as Shifted.
Right Shift Operation
Using Right Shift operation, we can shift one element of a list to the right rotationally. So if the list items are [a,b,c,d], then after right shifting, it will be [d, a, b,c]. So we will make a clause right_shift(L1, L2).
We're using append predicate to decompose the list so that concatenation of Front List and Last List results in original list. Using Prolog, backtracking, we're then right shifting the list.
This can also be used to check whether the two lists are shifted at one position or not.
Program (list_repos.pl)
right_shift([], []). right_shift(OriginalList, [Last|Front]) :- append(Front, [Last], OriginalList).
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, 1 lines read - 515 bytes written, 7 ms yes | ?- right_shift([a,b,c,d],L2). L2 = [d,a,b,c] yes | ?- right_shift([a,b,c,d,e],[e,a,b,c,d,e]). no | ?- right_shift([a,b,c,d,e],[e,a,b,c,d]). yes | ?-
Explanation
right_shift([], []) is base case where shifting empty list results in empty list.
right_shift(OriginalList, [Last|Front]) :- append(Front, [Last], OriginalList) shift the list in rotational manner.
OriginalList, [Last|Front] is to get the shifted result as a list having Front concatenated with Last List.
append(Front, [Last], OriginalList) call determines the Front elemeent and Last List whose concatenation results in original list by using backtracking technique.