
- 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 - 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.