1. introduction
2.arithmetic
3. if-else - CLICK
4.loop
5. List: click
6. CBCS university assignments CLICK
7. Read value from user:
1. BIRYINI IS A FOOD.
QUESTION:?
A) IS BIRIYANI A FOOD?
B) IS MOMO A FOOD?
2.
BIRYINI IS A FOOD.
MOMO IS A FOOD.
QUESTION:?
A) HOW MANY FOODS?
B) IF ALL FOODS ARE MEAL , THEN DISPLAY ALL MEALS.
3.
BIRYINI IS A FOOD.
MOMO IS A FOOD.
BIRIYANI IS A LUNCH.
QUESTION:?
A) WHICH ITEMS ARE IN LUNCH?
. Print "Hello World"
Description
This program prints the message Hello World on the screen.
Program
hello :-
write('Hello World!').
Run
?- hello.
Output
Hello World!
true.
Explanation
-
hellois a predicate. -
write()displays text on the screen. -
:-means "is defined as".
2. Input Your Name and Print It
Description
This program accepts a user's name and prints a greeting.
Program
greet :-
write('Enter your name: '),
read(Name),
write('Hello '),
write(Name).
Run
?- greet.
Sample Input
john.
Output
Enter your name:
john.
Hello john
true.
Explanation
-
read(Name)reads input from the keyboard. -
The entered value is stored in variable
Name. -
write(Name)displays the stored value.
3. Input Two Numbers and Display Them
Description
This program reads two numbers and displays them.
Program
display_numbers :-
write('Enter first number: '),
read(A),
write('Enter second number: '),
read(B),
write('First Number = '),
write(A),
nl,
write('Second Number = '),
write(B).
Run
?- display_numbers.
Sample Input
15.
25.
Output
Enter first number:
15.
Enter second number:
25.
First Number = 15
Second Number = 25
true.
Explanation
-
Astores the first number. -
Bstores the second number. -
nlprints a new line.
4. Input a Number and Print Its Square
Description
This program accepts a number and prints its square.
Program
square :-
write('Enter a number: '),
read(N),
S is N * N,
write('Square = '),
write(S).
Run
?- square.
Sample Input
8.
Output
Enter a number:
8.
Square = 64
true.
Explanation
-
read(N)reads the number. -
isevaluates arithmetic expressions. -
Sstores the square ofN.
5. Input Age and Display Eligibility to Vote
Description
This program checks whether a person is eligible to vote.
Program
vote :-
write('Enter your age: '),
read(Age),
Age >= 18,
write('Eligible to vote.').
vote :-
write('Enter your age: '),
read(Age),
Age < 18,
write('Not eligible to vote.').
Run
?- vote.
Sample Input
20.
Output
Enter your age:
20.
Eligible to vote.
true.
Sample Input
15.
Output
Enter your age:
15.
Not eligible to vote.
true.
Explanation
-
>=checks if the age is 18 or more. -
<checks if the age is less than 18. - Based on the condition, the appropriate message is printed.
Prolog Operators with Examples
Operators in Prolog are symbols used to perform arithmetic calculations, comparisons, logical operations, and term matching.
1. Arithmetic Operators
These operators perform mathematical calculations.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | X is 10 + 5. | X = 15 |
- | Subtraction | X is 10 - 5. | X = 5 |
* | Multiplication | X is 10 * 5. | X = 50 |
/ | Division | X is 10 / 5. | X = 2.0 |
// | Integer Division | X is 10 // 3. | X = 3 |
mod | Remainder | X is 10 mod 3. | X = 1 |
** | Power | X is 2 ** 3. | X = 8.0 |
Example Program
calculate :-
A is 20 + 10,
B is 20 - 10,
C is 20 * 10,
D is 20 / 10,
E is 20 // 3,
F is 20 mod 3,
G is 2 ** 4,
write(A), nl,
write(B), nl,
write(C), nl,
write(D), nl,
write(E), nl,
write(F), nl,
write(G).
Output
30
10
200
2.0
6
2
16.0
2. Arithmetic Comparison Operators
These operators compare numeric values.
| Operator | Meaning | Example | Result |
|---|---|---|---|
> | Greater than | 10 > 5 | True |
< | Less than | 5 < 10 | True |
>= | Greater than or equal | 10 >= 10 | True |
=< | Less than or equal | 5 =< 8 | True |
=:= | Arithmetic equality | 5+5 =:= 10 | True |
=\= | Arithmetic inequality | 10 =\= 5 | True |
Example
compare :-
X = 15,
Y = 10,
X > Y,
write('X is greater than Y').
Output
X is greater than Y
3. Unification Operators
These operators compare or assign terms.
| Operator | Meaning | Example |
|---|---|---|
= | Unify two terms | X = 10. |
\= | Not unifiable | 10 \= 20. |
== | Exactly identical | 10 == 10. |
\== | Not identical | 10 \== 20. |
Example
test :-
X = apple,
write(X).
Output
apple
4. Logical Operators
These operators combine conditions.
| Operator | Meaning | Example |
|---|---|---|
, | AND | A > 5, A < 20 |
; | OR | A = 5 ; A = 10 |
\+ | NOT | \+ (5 > 10) |
Example
check :-
X = 15,
X > 10,
X < 20,
write('Number is between 10 and 20').
Output
Number is between 10 and 20
5. Assignment Operator (is)
The is operator evaluates an arithmetic expression and assigns the result to a variable.
Syntax
Variable is Expression.
Example
square :-
N = 6,
S is N * N,
write(S).
Output
36
6. Example Program Using Multiple Operators
demo :-
write('Enter first number: '),
read(A),
write('Enter second number: '),
read(B),
Sum is A + B,
Product is A * B,
write('Sum = '),
write(Sum), nl,
write('Product = '),
write(Product), nl,
(A > B ->
write('First number is greater')
;
write('Second number is greater or equal')
).
Sample Run
?- demo.
Enter first number:
12.
Enter second number:
8.
Sum = 20
Product = 96
First number is greater
true.
Summary of Prolog Operators
| Category | Operators |
|---|---|
| Arithmetic | +, -, *, /, //, mod, ** |
| Comparison | >, <, >=, =<, =:=, =\= |
| Unification | =, \=, ==, \== |
| Logical | , (AND), ; (OR), \+ (NOT) |
| Assignment | is |
IF-ELSE in Prolog
Unlike languages such as C, C++, or Java, Prolog does not have a traditional if-else statement. Instead, it uses the If-Then-Else operator (-> ;).
Syntax
(Condition ->
Then_Part
;
Else_Part
).
Meaning
-
Condition→ Checks the condition. -
->→ Means THEN. -
;→ Means ELSE.
Example 1: Check Eligible to Vote
Program
vote :-
write('Enter your age: '),
read(Age),
(Age >= 18 ->
write('Eligible to Vote')
;
write('Not Eligible to Vote')
).
Run
?- vote.
Sample Input
20.
Output
Enter your age:
20.
Eligible to Vote
true.
Sample Input
15.
Output
Enter your age:
15.
Not Eligible to Vote
true.
Example 2: Find Larger Number
Program
largest :-
write('Enter first number: '),
read(A),
write('Enter second number: '),
read(B),
(A > B ->
write('Largest Number = '),
write(A)
;
write('Largest Number = '),
write(B)
).
Sample Run
?- largest.
Enter first number:
10.
Enter second number:
25.
Largest Number = 25
true.
Example 3: Check Even or Odd
Program
evenodd :-
write('Enter a number: '),
read(N),
(N mod 2 =:= 0 ->
write('Even Number')
;
write('Odd Number')
).
Sample Output
?- evenodd.
Enter a number:
18.
Even Number
true.
Example 4: Find Positive or Negative
Program
sign :-
write('Enter a number: '),
read(N),
(N >= 0 ->
write('Positive Number')
;
write('Negative Number')
).
Output
?- sign.
Enter a number:
-8.
Negative Number
true.
Example 5: Pass or Fail
Program
result :-
write('Enter marks: '),
read(Marks),
(Marks >= 40 ->
write('Pass')
;
write('Fail')
).
Output
?- result.
Enter marks:
65.
Pass
true.
Nested IF-ELSE Example (Grade Calculator)
Program
grade :-
write('Enter marks: '),
read(M),
(M >= 90 ->
write('Grade A')
;
(M >= 75 ->
write('Grade B')
;
(M >= 50 ->
write('Grade C')
;
write('Fail')
)
)
).
Sample Run
?- grade.
Enter marks:
82.
Grade B
true.
General Syntax
predicate :-
(Condition ->
Statement1
;
Statement2
).
Difference Between C and Prolog IF-ELSE
| C/C++ | Prolog |
|---|---|
if(condition) | (Condition -> ...) |
else | ; |
{ } block | Parentheses ( ) |
Statement ends with ; | Predicate ends with . |
What is a List in Prolog?
A list is a collection of elements enclosed in square brackets [ ].
- A list can contain numbers, atoms, variables, or even other lists.
- Elements are separated by commas.
Syntax
[Element1, Element2, Element3, ...]
Examples
[1,2,3,4,5]
[apple, mango, orange]
[a,10,hello,25]
[]
Characteristics of a List
- Ordered collection of elements.
-
Elements are enclosed within
[ ]. - Elements are separated by commas.
- A list may contain duplicate values.
- Lists can contain different data types.
Example
[10,20,30]
[apple,25,mango]
[]
Head and Tail of a List
A list consists of:
- Head → First element
- Tail → Remaining elements
Syntax
[Head|Tail]
Example
[10,20,30,40]
Head
10
Tail
[20,30,40]
Example Program
show :-
L = [10,20,30,40],
L = [H|T],
write('Head = '),
write(H), nl,
write('Tail = '),
write(T).
Run
?- show.
Output
Head = 10
Tail = [20,30,40]
true.
Empty List
An empty list contains no elements.
Syntax
[]
Example
empty :-
L=[],
write(L).
Output
[]
List Matching (Pattern Matching)
Prolog matches list elements using variables.
Example
match :-
[A,B,C]=[100,200,300],
write(A),nl,
write(B),nl,
write(C).
Output
100
200
300
Check Membership using member/2
The member/2 predicate checks whether an element exists in a list.
Syntax
member(Element,List)
Example
check :-
member(30,[10,20,30,40]).
Run
?- check.
Output
true.
Another Example
?- member(mango,[apple,mango,banana]).
Output
true.
Append Two Lists
The append/3 predicate joins two lists.
Syntax
append(List1,List2,Result).
Example
join :-
append([1,2,3],[4,5,6],L),
write(L).
Output
[1,2,3,4,5,6]
Length of a List
The length/2 predicate counts the number of elements.
Example
size :-
length([10,20,30,40,50],N),
write(N).
Output
5
Reverse a List
The reverse/2 predicate reverses a list.
Example
rev :-
reverse([10,20,30,40],R),
write(R).
Output
[40,30,20,10]
Sort a List
The sort/2 predicate arranges elements in ascending order.
Example
sorting :-
sort([5,2,8,1,6],L),
write(L).
Output
[1,2,5,6,8]
Find Maximum Element
maximum :-
max_list([15,28,9,45,30],M),
write(M).
Output
45
Find Minimum Element
minimum :-
min_list([15,28,9,45,30],M),
write(M).
Output
9
Sum of List Elements
sum :-
sum_list([10,20,30,40],S),
write(S).
Output
100
Nested List
Lists can contain other lists.
Example
[1,2,[3,4],5]
Another Example
[[a,b],[c,d],[e,f]]
Access Head and Tail
headtail :-
[H|T]=[apple,mango,banana,orange],
write(H),nl,
write(T).
Output
apple
[mango,banana,orange]
Generate List Members
show :-
member(X,[10,20,30]),
write(X),nl,
fail.
show.
Output
10
20
30
true.
Common Built-in List Predicates
| Predicate | Description | Example |
|---|---|---|
member/2 | Checks whether an element exists | member(5,[1,2,5]) |
append/3 | Joins two lists | append([1],[2],L) |
length/2 | Counts elements | length([1,2,3],N) |
reverse/2 | Reverses a list | reverse([1,2],R) |
sort/2 | Sorts elements | sort([4,2,1],L) |
max_list/2 | Finds the largest element | max_list([2,5,3],M) |
min_list/2 | Finds the smallest element | min_list([2,5,3],M) |
sum_list/2 | Calculates the sum of elements | sum_list([1,2,3],S) |
Advantages of Lists
- Easy to store multiple values.
- Dynamic size (can grow or shrink).
- Supports recursion efficiently.
- Useful for searching and pattern matching.
- Widely used in Artificial Intelligence and symbolic programming.
What is Cut (!)?
The Cut operator (!) is a special control operator in Prolog.
It is used to stop backtracking and prevent Prolog from searching for alternative solutions after the cut is reached.
Definition
The Cut (
!) is a control operator that commits Prolog to the choices made before it and prevents further backtracking beyond that point.
Syntax
Predicate :-
Goal1,
Goal2,
!,
Goal3.
Why Do We Use Cut?
- Improves program efficiency.
- Stops unnecessary backtracking.
- Makes the program execute faster.
- Selects only one solution.
- Prevents Prolog from trying other rules.
How Cut Works
Without Cut
Goal
│
▼
Rule 1
│
▼
Failure
│
▼
Backtracking
│
▼
Rule 2
With Cut
Goal
│
▼
Rule 1
│
▼
!
│
▼
No Backtracking
│
▼
Program Ends
Example 1: Simple Cut
Program
largest(X,Y,X) :-
X >= Y,
!.
largest(_,Y,Y).
Run
?- largest(20,15,R).
Output
R = 20
Explanation
-
20 >= 15is true. -
Prolog reaches
!. - Backtracking stops.
- The second rule is ignored.
Example 2: Without Cut
largest(X,Y,X) :-
X >= Y.
largest(_,Y,Y).
Run
?- largest(20,15,R).
Output
R = 20 ;
R = 15.
Explanation
Since there is no Cut, Prolog also tries the second rule during backtracking.
Example 3: Check Positive Number
positive(X) :-
X > 0,
!,
write('Positive Number').
positive(_) :-
write('Not Positive').
Run
?- positive(15).
Output
Positive Number
true.
Another Run
?- positive(-5).
Output
Not Positive
true.
Example 4: Maximum of Two Numbers
max(A,B,A) :-
A >= B,
!.
max(_,B,B).
Run
?- max(50,20,R).
Output
R = 50
Example 5: Grade Calculator
grade(Marks) :-
Marks >= 90,
!,
write('Grade A').
grade(Marks) :-
Marks >= 75,
!,
write('Grade B').
grade(Marks) :-
Marks >= 50,
!,
write('Grade C').
grade(_) :-
write('Fail').
Run
?- grade(82).
Output
Grade B
true.
Example 6: Voting Eligibility
vote(Age) :-
Age >= 18,
!,
write('Eligible to Vote').
vote(_) :-
write('Not Eligible').
Run
?- vote(20).
Output
Eligible to Vote
Example 7: Even or Odd
evenodd(N) :-
N mod 2 =:= 0,
!,
write('Even Number').
evenodd(_) :-
write('Odd Number').
Run
?- evenodd(12).
Output
Even Number
Cut with If-Then-Else
largest :-
write('Enter First Number: '),
read(A),
write('Enter Second Number: '),
read(B),
(A > B ->
write('Largest = '),
write(A)
;
write('Largest = '),
write(B)
).
This example uses If-Then-Else (-> ;) and does not require Cut, because the conditional already commits to one branch.
Types of Cut
1. Green Cut
A Green Cut improves efficiency without changing the logical meaning of the program.
Example
max(X,Y,X) :-
X >= Y,
!.
max(_,Y,Y).
Purpose: Prevents unnecessary backtracking while preserving correct results.
2. Red Cut
A Red Cut changes the logical behavior of the program. Using or removing it may change the answers produced.
Example
grade(Marks) :-
Marks >= 40,
!,
write('Pass').
grade(_) :-
write('Fail').
Here, the cut commits to the first matching rule, affecting how later rules are considered.
Advantages of Cut
- Stops unnecessary backtracking.
- Makes execution faster.
- Improves program efficiency.
- Reduces memory usage.
- Produces a single desired solution.
- Simplifies control flow in some programs.
Disadvantages of Cut
- Can make programs harder to understand.
- May change the logical meaning if used incorrectly.
- Makes debugging more difficult.
- Overuse can reduce program readability.
Difference Between Cut and If-Else
Cut (!) | If-Then-Else (-> ;) |
|---|---|
| Stops backtracking | Selects one branch based on a condition |
| Used for execution control | Used for conditional decisions |
| Can affect later rule selection | Does not rely on multiple clauses |
| May improve efficiency | Improves readability for conditions |
Summary
| Symbol | Meaning |
|---|---|
! | Cut operator |
| Purpose | Stops backtracking |
| Benefit | Improves efficiency and selects a single solution |
| Types | Green Cut, Red Cut |
| Main Use | Control execution and avoid unnecessary alternative solutions |
No comments:
Post a Comment