Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

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.

Advertisements