
- 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 - Decomposing Structures Predicates
Decompsing Structures Predicates are built-in predicates which are used to check Functors of terms. Following is the list of important decomposing structures predictates.
Predicate | Description |
---|---|
functor(T,F,N) | returns true if F is the principal functor of T, and N is the arity of F. |
arg(N,Term,A) | returns true if A is the Nth argument in Term. Otherwise returns false. |
..L | returns true if L is a list that contains the functor of Term, followed by its arguments. |
The functor(T,F,N) Predicate
This returns true if F is the principal functor of T, and N is the arity of F.
Note − Arity means the number of attributes.
Example
| ?- functor(t(f(X),a,T),Func,N). Func = t N = 3 yes | ?-
The arg(N,Term,A) Predicate
This returns true if A is the Nth argument in Term. Otherwise returns false.
Example
| ?- arg(1,t(t(X),[]),A). A = t(X) yes | ?- arg(2,t(t(X),[]),A). A = [] yes | ?-
Now, let us see another example. In this example, we are checking that the first argument of D will be 12, the second argument will be apr and the third argument will be 2020.
Example
| ?- functor(D,date,3), arg(1,D,12), arg(2,D,apr), arg(3,D,2020). D = date(12,apr,2020) yes | ?-
The ../2 Predicate
This is another predicate represented as double dot (..). This takes 2 arguments, so /2 is written. So Term = .. L, this is true if L is a list that contains the functor of Term, followed by its arguments.
Example
| ?- f(a,b) =.. L. L = [f,a,b] yes | ?- T =.. [is_blue,sam,today]. T = is_blue(sam,today) yes | ?-
By representing the component of a structure as a list, they can be recursively processed without knowing the functor name. Let us see another example −
Example
| ?- f(2,3)=..[F,N|Y], N1 is N*3, L=..[F,N1|Y]. F = f L = f(6,3) N = 2 N1 = 6 Y = [3] yes | ?-