
- 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 - Examples of Cuts
In this section, we will see some examples of cuts in prolog. Let us consider, we want to find the maximum of two elements. So we will check these two conditions.
If X > Y, then Max := X
if X <= Y, then Max := Y
Now from these two lines, we can understand that these two statements are mutually exclusive, so when one is true, another one must be false. In such cases we can use the cut. So let us see the program.
We can also define a predicate where we use the two cases using disjunction (OR logic). So when first one satisfies, it does not check for the second one, otherwise, it will check for the second statement.
Program (cut_example.pl)
max(X,Y,X) :- X >= Y,!. max(X,Y,Y) :- X < Y. max_find(X,Y,Max) :- X >= Y,!, Max = X; Max = Y.
Output
| ?- consult('D:/TP Prolog/Sample Codes/cut_example.pl'). compiling D:/TP Prolog/Sample Codes/cut_example.pl for byte code... D:/TP Prolog/Sample Codes/cut_example.pl compiled, 2 lines read - 1195 bytes written, 4 ms yes | ?- trace. The debugger will first creep -- showing everything (trace) yes {trace} | ?- max(10,20,Max). 1 1 Call: max(10,20,_23) ? 2 2 Call: 10>=20 ? 2 2 Fail: 10>=20 ? 2 2 Call: 10<20 ? 2 2 Exit: 10<20 ? 1 1 Exit: max(10,20,20) ? Max = 20 yes {trace} | ?- max_find(20,10,Max). 1 1 Call: max_find(20,10,_23) ? 2 2 Call: 20>=10 ? 2 2 Exit: 20>=10 ? 1 1 Exit: max_find(20,10,20) ? Max = 20 yes {trace} | ?- notrace. The debugger is switched off yes | ?-
Let us see another example, where we will use list. In this program we will try to insert an element into a list, if it is not present in the list before. And if the list has the element before we will simply cut it. For the membership checking also, if the item is at the head part, we should not check further, so cut it, otherwise check into the tail part.
Program(cut_example.pl)
list_member(X,[X|_]) :- !. list_member(X,[_|TAIL]) :- list_member(X,TAIL). list_append(A,T,T) :- list_member(A,T),!. list_append(A,T,[A|T]).
Output
| ?- consult('D:/TP Prolog/Sample Codes/cut_example.pl'). compiling D:/TP Prolog/Sample Codes/cut_example.pl for byte code... D:/TP Prolog/Sample Codes/cut_example.pl compiled, 3 lines read - 898 bytes written, 3 ms yes | ?- trace. The debugger will first creep -- showing everything (trace) yes {trace} | ?- list_append(a,[a,b,c,d,e], L). 1 1 Call: list_append(a,[a,b,c,d,e],_33) ? 2 2 Call: list_member(a,[a,b,c,d,e]) ? 2 2 Exit: list_member(a,[a,b,c,d,e]) ? 1 1 Exit: list_append(a,[a,b,c,d,e],[a,b,c,d,e]) ? L = [a,b,c,d,e] yes {trace} | ?- list_append(k,[a,b,c,d,e], L). 1 1 Call: list_append(k,[a,b,c,d,e],_33) ? 2 2 Call: list_member(k,[a,b,c,d,e]) ? 3 3 Call: list_member(k,[b,c,d,e]) ? 4 4 Call: list_member(k,[c,d,e]) ? 5 5 Call: list_member(k,[d,e]) ? 6 6 Call: list_member(k,[e]) ? 7 7 Call: list_member(k,[]) ? 7 7 Fail: list_member(k,[]) ? 6 6 Fail: list_member(k,[e]) ? 5 5 Fail: list_member(k,[d,e]) ? 4 4 Fail: list_member(k,[c,d,e]) ? 3 3 Fail: list_member(k,[b,c,d,e]) ? 2 2 Fail: list_member(k,[a,b,c,d,e]) ? 1 1 Exit: list_append(k,[a,b,c,d,e],[k,a,b,c,d,e]) ? L = [k,a,b,c,d,e] (16 ms) yes {trace} | ?- notrace. The debugger is switched off yes | ?-