0% found this document useful (0 votes)
4 views

AI Codes

The document contains a list of Prolog programs addressing various problems such as palindrome checking, string length calculation, list membership, list appending, element insertion and deletion, arithmetic operations for student and employee details, family tree relationships, recursion, and the Monkey Banana and 8-Queens problems. Each program includes definitions and predicates to solve the respective tasks. Outputs for each program are indicated but not explicitly shown in the document.

Uploaded by

dharmatej0504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

AI Codes

The document contains a list of Prolog programs addressing various problems such as palindrome checking, string length calculation, list membership, list appending, element insertion and deletion, arithmetic operations for student and employee details, family tree relationships, recursion, and the Monkey Banana and 8-Queens problems. Each program includes definitions and predicates to solve the respective tasks. Outputs for each program are indicated but not explicitly shown in the document.

Uploaded by

dharmatej0504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Program List

1. Write a prolog program to implement the Palindrome.


Ans:
palind([]):- write('palindrome').
palind([_]):- write('palindrome').
palind(L) :-
append([H|T], [H], L),
palind(T)
;
write('Not a palindrome').
Output:

2. Write a prolog program to find the length of the string (List).


Ans:
% length of empty list is 0 (base case)
list_length([], 0).
list_length([_ | L], N) :-
list_length(L, N1),
N is N1 + 1.
% If length of L is N1, then length of [_ | L] will be N1 + 1
Output:

2.1 Determine whether an element X belongs to a list L


Ans:
is_member(X, [X | _]) :- !. % If the head of the list is X
is_member(X, [_ | Rest]) :- % else recur for the rest of the list
is_member(X, Rest).
Output:

2.2 Append a list L2 at the end of another list L1 and put the resultant list in L3
Ans:
% If L1 is empty, resultant list will be equal to L2 (base case)
append_list([], L2, L2).
append_list([X | L1], L2, [X | L3]) :-
append_list(L1, L2, L3).
Output:

2.3 Insert an element X at the end of a list L


Ans:
insert_end(L, X, NewL) :-
append_list(L, [X], NewL).
Output:

2.4 Delete the first occurrence of an element X from a list L


Ans:
% Deletion of any element from empty list will produce empty list(base case)
delete_element(_, [], []).
delete_element(X, [X | L], L) :- !.
delete_element(X, [Y | L], [Y | L1]) :-
delete_element(X, L, L1).
Output:
3. write a program for studying usage of arithmetic operators in prolog.
1. accept name of the student, roll no, his subject name, maximum marks and
obtained marks in the subject. compute the percentage of the student, display
his result with other information.
Ans:
% Define a predicate to calculate percentage and display student details
student_details(Name, RollNo, Subject, MaxMarks, ObtMarks) :-
Percentage is (ObtMarks / MaxMarks) * 100,
format("Student Name: ~w\n", [Name]),
format("Roll No: ~w\n", [RollNo]),
format("Subject: ~w\n", [Subject]),
format("Maximum Marks: ~w\n", [MaxMarks]),
format("Obtained Marks: ~w\n", [ObtMarks]),
format("Percentage: ~2f\n", [Percentage]),
(Percentage >= 40 -> format("Result: Pass\n"); format("Result: Fail\n")).
Output:

2. Accept Department, designation, name, age, basic salary, House Rent


Allowance(HRA) of an employee compute dearest allowance(DA) which is
15% of basic salary. Determine the gross salary (basic salary+HRA+DA) of the
employee. Display all information of the employee(Generate Payslip).
Ans:
% Define a predicate to calculate DA, gross salary, and display employee
details
employee_payslip(Department, Designation, Name, Age, BasicSalary, HRA) :-
DA is 0.15 * BasicSalary,
GrossSalary is BasicSalary + HRA + DA,
format("Department: ~w\n", [Department]),
format("Designation: ~w\n", [Designation]),
format("Employee Name: ~w\n", [Name]),
format("Age: ~w\n", [Age]),
format("Basic Salary: ~2f\n", [BasicSalary]),
format("House Rent Allowance (HRA): ~2f\n", [HRA]),
format("Dearness Allowance (DA): ~2f\n", [DA]),
format("Gross Salary: ~2f\n", [GrossSalary]).

Output:

4. Write a program for usage of rules in Prolog. Create a family tree program to
include following rules.
1. M is the mother of P if she is a parent of P and is female.
2. F is the father of P if he is a parent of P and is male.
3. X is a sibling of Y if they both have the same parent.
4. Then add rules for grandparents, uncle-aunt, sister and brother.
Ans:
% Facts defining family relationships
parent(john, alice).
parent(john, bob).
parent(mary, alice).
parent(mary, bob).
parent(bob, charlie).
parent(bob, daisy).
parent(alice, edward).
parent(alice, fiona).
male(john).
male(bob).
male(charlie).
male(edward).
female(mary).
female(alice).
female(daisy).
female(fiona).

% Rules defining relationships


mother(M, P) :- parent(M, P), female(M).
father(F, P) :- parent(F, P), male(F).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
sister(S, X) :- sibling(S, X), female(S).
brother(B, X) :- sibling(B, X), male(B).
grandparent(GP, X) :- parent(GP, P), parent(P, X).
grandmother(GM, X) :- grandparent(GM, X), female(GM).
grandfather(GF, X) :- grandparent(GF, X), male(GF).
uncle(U, X) :- brother(U, P), parent(P, X).
aunt(A, X) :- sister(A, P), parent(P, X).
Output:

5. Implement a program for recursion and list in prolog.


Ans:
% Recursive predicate to compute factorial
factorial(0, 1).
factorial(N, Result) :-
N > 0,
N1 is N - 1,
factorial(N1, R1),
Result is N * R1.
Output:
Recursion for list:
% Predicate to compute the sum of a list
sum_list([], 0).
sum_list([H|T], Sum) :-
sum_list(T, RestSum),
Sum is H + RestSum.
Output:

1. Write a program to implement Monkey Banana Problem.


Answer:
% Define actions that change the state
move(state(middle, on_box, middle, has_not), grasp, state(middle, on_box,
middle, has)).
move(state(P, on_floor, P, H), climb, state(P, on_box, P, H)).
move(state(P1, on_floor, P1, H), push(P1, P2), state(P2, on_floor, P2, H)).
move(state(P1, on_floor, B, H), walk(P1, P2), state(P2, on_floor, B, H)).

% Define a plan to reach the goal state


can_get(state(_, _, _, has)).
can_get(State1) :-
move(State1, Action, State2),
write(Action), nl,
can_get(State2).
Output:
2. Program to implement 8-Queens Problem.
Answer:
% Main predicate to solve the N-Queens problem
eight_queens(Solution) :-
length(Solution, 8), % Ensure the solution has 8 elements (one per row)
Solution = [1,2,3,4,5,6,7,8], % Generate permutations for queen placements
permutation(Solution, Board),
safe(Board), % Check for safety constraints
write(Board), nl.
% Check if all queens are placed safely
safe([]).
safe([Queen|Others]) :-
safe_queen(Queen, Others, 1),
safe(Others).
% Check if a queen is safe from all other queens
safe_queen(_, [], _).
safe_queen(Q1, [Q2|Others], Dist) :-
Q1 =\= Q2 + Dist, % Check diagonals
Q1 =\= Q2 - Dist,
NextDist is Dist + 1,
safe_queen(Q1, Others, NextDist).
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy