
- 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 - Finding Minimum of A List
This operation is used to find the minimum element from a list. We will define a predicate, list_min_elem(List, Min), then this will find Min element from the list and return.
If there is only one element, then it will be the min element.
Divide the list as [X,Y|Tail]. Now recursively find min of [Y|Tail] and store it into MinRest, and store minimum of X and MinRest, then store it to Min.
Program (list_misc.pl)
min_of_two(X,Y,X) :- X =< Y. min_of_two(X,Y,Y) :- X > Y. list_min_elem([X],X). list_min_elem([X,Y|Rest],Min) :- list_min_elem([Y|Rest],MinRest), min_of_two(X,MinRest,Min).
Output
| ?- consult('D:/TP Prolog/Sample Codes/list_misc.pl'). compiling D:/TP Prolog/Sample Codes/list_misc.pl for byte code... D:/TP Prolog/Sample Codes/list_misc.pl compiled, 3 lines read - 1086 bytes written, 6 ms yes | ?- list_min_elem([8,5,3,4,7,9,6,1],Min). Min = 1 ? yes | ?- list_min_elem([5,12,69,112,48,4],Min). Min = 4 ? yes | ?-
Explanation
min_of_two(X,Y,X) :- X =< Y is a helper function which checks X as same or less than Y then Min is X.
min_of_two(X,Y,Y) :- X > Y. It is another case where if X is greater than Y then Min is Y.
list_min_elem([X],X). Minimum of one element List is the only item of the List.
-
list_min_elem([X,Y|Rest],Min) :- list_min_elem([Y|Rest],MinRest), min_of_two(X,MinRest,Min). returns Min as the lowest element of the list.
list_min_elem([X,Y|Rest],Min) is to divide the first list where X, Y represents the first two elements of the list and Rest represents the tail of the list. Min is the result as Minimum element of the list.
list_min_elem([Y|Rest],MinRest) makes a recursive call to compare the list without head X and get the next lowest value as MinRest.
min_of_two(X,MinRest,Min) compares X and MinRest and then return the Min as lowest value.