Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

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.

Advertisements