Programming of Arithmetic Operators and Predicates and Programming to use Append Using Structures
Programming of Arithmetic Operators and Predicates
Prolog is used to develop Information Systems other than the Expert Systems. Therefore, it supports the arithmetic operators like procedural programming languages, such as C, C++ and JAVA. Common arithmetic operators are as follows:
| Arithmetic Operator | Meaning | Example |
| + | Addition | 1+2=3 |
| - | Subtraction | 12-7=5 |
| * | Multiplication | 2*3=6 |
| / | Floating Point Division | 8/3=2.66 |
| // | Integer Division | 8/3=2 |
| % | Mod (Remainder) | 5%2= 1 |
| ** | Power | 2**2=4 |
Relation operators are called as arithmetic predicates using Prolog. Common arithmetic predicates are as follows:
| Expression | Symbols | Example |
| Evaluate | is | X is 2+2. Result: X= 4 Yes |
| Equal To | = : = | 4-1 = : = 1+2. Result: Yes |
| Greater Than | > | X is 2+2, X > 5. Result: 4 Yes |
| Greater Than and Equal To | >= | X is 2+3, X >= 5. Result: 5 Yes |
| Less Than | < | X is 1+2, X < 4. Result: 3 Yes |
| Less Than and Equal To | =< | X is 2+2, X =< 4. Result: 4 Yes |
| Comparison operators to assign value | = | X+2 = 1+Y. Result: X = 1 , Y = 2 Yes |
Example #1
Develop a program using Prolog to add two numbers based on the user input:
| /*Declare Rule To Add Two Numbers*/ |
| add(Sum,Num1,Num2):- |
| Sum is Num1+Num2 |
Example #2
Develop a program using Prolog to calculate area of rectangle based on the user input:
| /*Declare Rule To Compute Area*/ |
| compute(Area,Length,Width):- |
| write(‘Area of Rectangle is ‘), |
| Area is Length*Width. |
Example #3
Develop a program using Prolog to calculate area, diameter and circumference of circle. Formulas are shown below to calculate area, diameter and circumference:
Area of circle is PI*Radius*Radius.
Diameter of circle is 2*Radius.
Circumference of circle is 2*PI*Radius.
| /*Declare Rule To Compute Area, Diameter and Circumference*/ |
| compute(Area,Dia,Circum, Radius):- |
| Area is (3.1416*Radius**2), |
| Dia is (2*Radius), |
| Circum is (2*3.1416*Radius). |
Programming to use Append Using Structures
Structure [Thing | Room] is indicating that variable ‘Thing’ is bounded to the first element of list where as variable ‘Room’ is bounded to remaining elements of list. Syntax of a query is as follows:
? [Thing | Room] = [apple, knife, crackers]
Thing = apple
Room = [knife, crackers]
Predicate used to construct or segregate the lists is called ‘Append’. Syntax to use append predicate is as follows:
? append ([apple, banana],[knife, crackers],Things).
Things = [apple, banana, knife, crackers]
Related posts:
- Programming to Use Rules Inside the Rules and Data Structure and Cut Predicate Programming to Use Rules Inside the Rules Variables declared by...
- Programming of Rules and Programming to Fail a Goal Programming of Rules Rules are relationships of facts by providing...
- Prolog Programming Prolog is a declarative programming language used most widely for...
- Sequence Programming Using VB 6.0 Sequence is a method of writing programs in a particular...
- Programming of Facts Programmer declares Facts by writing Predicate first and then fact...