Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Order of a List



In prolog, checking the order of List means to check if a List elements are in ascending/decending order or not.In this chapter, we're creating predicates to check both ascending and descending orders of a List.

Ascending Order Operation

Here we will define a predicate list_order_asc(L) which checks whether L is ordered in ascending order or not. So if L = [1,2,3,4,5,6], then the result will be true. And if L = [2,4,3,1] then List is not in order thus result will be false.

  • If there is only one element, that is already ordered.

  • Otherwise take first two elements X and Y as Head, and rest as Tail. If X =< Y, then call the clause again with the parameter [Y|Tail], so this will recursively check from the next element.

Program (list_repos.pl)

list_order_asc([X, Y | Tail]) :- X =< Y, list_order_asc([Y|Tail]).
list_order_asc([X]).

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:2: warning: singleton variables [X] for list_order_asc/1
D:/TP Prolog/Sample Codes/list_repos.pl compiled, 1 lines read - 654 bytes written, 4 ms

yes
| ?- list_order_asc([1,2,3,4,5,6,6,7,7,8]).

true ? a

no
| ?- list_order_asc([1,4,2,3,6,5]).

no
| ?- 

Explanation

  • list_order_asc([X, Y | Tail]) :- X =< Y, list_order_asc([Y|Tail]) represents the recursive case.

    • [X, Y | Tail] is to divide the first list where X,Y represent the first two elements of the list and Tail represents the tail of the list.

    • X =< Y is comparing X with Y.

    • list_order_asc([Y|Tail]) is to check the rest of the list.

Descending Order Operation

Here we will define a predicate list_order_asc(L) which checks whether L is ordered in descending order or not. So if L = [5,4,3,2,1], then the result will be true. And if L = [4,3,1,2] then List is not in required order thus result will be false.

  • If there is only one element, that is already ordered.

  • Otherwise take first two elements X and Y as Head, and rest as Tail. If X =< Y, then call the clause again with the parameter [Y|Tail], so this will recursively check from the next element.

Program (list_repos.pl)

list_order_desc([X, Y | Tail]) :- X >= Y, list_order_desc([Y|Tail]).
list_order_desc([X]).

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:2: warning: singleton variables [X] for list_order_desc/1
D:/TP Prolog/Sample Codes/list_repos.pl compiled, 1 lines read - 679 bytes written, 3 ms

yes
| ?- list_order_desc([7,6,5,4,3,2,1]).

true ? a

no
| ?- list_order_desc([5,4,2,6,7,1]).

no
| ?- 

Explanation

  • list_order_desc([X, Y | Tail]) :- X >= Y, list_order_desc([Y|Tail]) represents the recursive case.

    • [X, Y | Tail] is to divide the first list where X,Y represent the first two elements of the list and Tail represents the tail of the list.

    • X =< Y is comparing X with Y.

    • list_order_desc([Y|Tail]) is to check the rest of the list.

Advertisements