0% found this document useful (0 votes)
4 views17 pages

Prolog Record

The document is a certificate from JNTUH College of Engineering certifying the completion of a Skill Development - Prolog Lab record for a B.Tech student in the academic year 2023-24. It includes a list of Prolog programs covering various problems such as the Monkey Banana problem, Medical Diagnosis, and the Traveling Salesman problem, along with their implementations. Additionally, the document discusses concepts like green and red cuts in Prolog, along with their advantages and disadvantages.
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 views17 pages

Prolog Record

The document is a certificate from JNTUH College of Engineering certifying the completion of a Skill Development - Prolog Lab record for a B.Tech student in the academic year 2023-24. It includes a list of Prolog programs covering various problems such as the Monkey Banana problem, Medical Diagnosis, and the Traveling Salesman problem, along with their implementations. Additionally, the document discusses concepts like green and red cuts in Prolog, along with their advantages and disadvantages.
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/ 17

JNTUH COLLEGE OF ENGINEERING

(Autonomous)
HYDERABAD-500072
BTech CSE(AI&ML)

CERTIFICATE
This is to certify that of B.Tech II year II Semester with
the Hall-Ticket number has successfully completed his/her
SKILL DEVELOPMENT- PROLOG Lab record for the academic year 2023-24.

Internal Examiner External Examiner

1
LIST OF PROGRAMS

SNO Name of Program Page No

1 Program for simple facts

2 Convert celsius to fahrenheit

3 Monkey Banana problem

4 Medical Diagnosis

5 4 - Queen problem

6 Traveling Salesman problem

7 Water Jug problem

8 Insert and Delete Nth element from list

9 Trees using lists

10 Count number of empty and non empty lists within a


list

11 Count an element within a list

2
1. Write simple fact for following:

● Ram likes mango.


● Seema is a girl.
● Bill likes Cindy.
● Rose is red.
● John owns gold

likes(ram,mango).
likes(bill,cindy).
red(rose).
own(john,gold).
girl(seema).

O/P:-

2. Write predicates one converts centigrade temperatures to Fahrenheit, the


other checks if a temperature is below freezing.

3
temp:- write('enter temperature in celsius:'),nl,
read(T),FT is (9/5*T)+32,write(FT).

3. Write a program to solve the Monkey Banana problem

% Define the initial state


% state(monkey_position, monkey_status, box_position, monkey_has_banana)

initial_state(state(at_door, onfloor, at_window, hasnot)).

% Define the goal state


goal_state(state(_, _, _, has)).

% Define the possible actions


move(state(middle, onbox, middle, hasnot),
grasp,
state(middle, onbox, middle, has)).
move(state(P, onfloor, P, H),
climb,
state(P, onbox, P, H)).
move(state(P1, onfloor, P1, H),
drag(P1, P2),
state(P2, onfloor, P2, H)).
move(state(P1, onfloor, B, H),
walk(P1, P2),
state(P2, onfloor, B, H)).

% Define the plan to reach the goal


canget(State, Actions, Actions) :-
goal_state(State).

4
canget(State1, CurrentActions, Actions) :-
move(State1, Action, State2),
\+ member(Action, CurrentActions), % Avoid loops
canget(State2, [Action | CurrentActions], Actions).

% Find the plan from the initial state


find_plan :-
initial_state(State),
canget(State, [], Actions),
reverse(Actions, CorrectOrderActions),
write('Plan: '), write(CorrectOrderActions), nl.

4. WAP in turbo prolog for medical diagnosis and show the advantages
and disadvantages of green and red cuts

Green Cuts (!)

Advantages:

Improved Efficiency: When you're certain a successful clause has been found, a
green cut prevents Prolog from backtracking and exploring further alternatives.
This can significantly improve performance, especially for programs with many
clauses.

Disadvantages:

5
Incorrect Results: If the clause chosen before the green cut is wrong, the
program will miss potentially valid solutions. Green cuts limit backtracking, which
is the process of exploring different paths in the search tree.

Reduced Readability: Excessive use of green cuts can make code harder to
understand, especially for someone not familiar with their behavior.

Red Cuts (\+)

Advantages:

Pruning Unnecessary Branches: Red cuts prevent Prolog from trying clauses that
begin with a specified predicate. This can be helpful for eliminating irrelevant
branches of the search tree, leading to faster execution.

Disadvantages:

Missing Valid Solutions: If the red cut is too broad, it might exclude clauses that
could have led to a valid solution. This can cause the program to miss potential
results.

Reduced Flexibility: Red cuts can make code less flexible, as they can potentially
eliminate alternative solutions that might be relevant in certain situations.

%to start the program first type go.

go:-got,write("do you want to try it oncce more(y/n)"),read(X),X='y',got.

got:-write("Enter patient name "),read(P),check(P,Dis),write(P),write(" has


"),write(Dis),nl.

symptom(P,fever):-write("Does "),write(P),write(" has fever(y/n)"),read(T),T='y'.

symptom(P,body_ache):-write("Does "),write(P),write(" has body


ache(y/n)"),read(T),T='y'.

symptom(P,runny_nose):-write("Does "),write(P),write(" has runny


nose(y/n)"),read(T),T='y'.

symptom(P,cough):-write("Does "),write(P),write(" has cough(y/n)"),read(T),T='y'.

6
symptom(P,fatigue):-write("Does "),write(P),write(" has
fatigue(y/n)"),read(T),T='y'.

symptom(P,ear_ache):-write("Does "),write(P),write(" has ear


ache(y/n)"),read(T),T='y'.

symptom(P,pressure_on_face):-write("Does "),write(P),write(" has pressure


around head(y/n)"),read(T),T='y'.

check(P,common_cold):-symptom(P,runny_nose),symptom(P,body_ache),sympto
m(P,cough),!.

check(P,flu):-symptom(P,fever),symptom(P,body_ache),!.

check(P,ear_infection):-symptom(P,fever),symptom(P,ear_ache),!.

check(P,sinus):-symptom(P,pressure_on_face),symptom(P,fatigue),symptom(P,ru
nny_nose),!.

check(P,no_disease),!.

O/P:-

7
5. Write a program to solve the 4-Queen problem.

queens(N, Queens) :-

length(Queens, N),

board(Queens, Board, 0, N, _, _),

queens(Board, 0, Queens).

board([], [], N, N, _, _).

board([_|Queens], [Col-Vars|Board], Col0, N, [_|VR], VC) :-

Col is Col0+1,

functor(Vars, f, N),

constraints(N, Vars, VR, VC),

8
board(Queens, Board, Col, N, VR, [_|VC]).

constraints(0, _, _, _) :- !.

constraints(N, Row, [R|Rs], [C|Cs]) :-

arg(N, Row, R-C),

M is N-1,

constraints(M, Row, Rs, Cs).

queens([], _, []).

queens([C|Cs], Row0, [Col|Solution]) :-

Row is Row0+1,

select(Col-Vars, [C|Cs], Board),

arg(Row, Vars, Row-Row),

queens(Board, Row, Solution).

O/P:-

6. Write a program to solve traveling salesman problems.

road("tampa","houston",200).

road("gordon","tampa",300).

road("houston","gordon",100).

9
road("houston","kansas_city",120).

road("gordon","kansas_city",130).

route(Town1,Town2,Distance):-

road(Town1,Town2,Distance).

route(Town1,Town2,Distance):-

road(Town1,X,Dist1),

route(X,Town2,Dist2),

Distance is Dist1+Dist2,

!.

O/P:-

10
7.Write a program to solve water jug problems using Prolog.

water_jug(X,Y):-X>4,Y<3,write('4L water jug overflowed.'),nl.

water_jug(X,Y):-X<4,Y>3,write('3L water jug overflowed.'),nl.

water_jug(X,Y):-X>4,Y>3,write('Both water jug overflowed.'),nl.

water_jug(X,Y):-(X=:=0,Y=:=0,nl,write('4L:0 & 3L:0 (Action :Fill 3L jug.)'),YY is 3


,water_jug(X,YY));

(X=:=0,Y=:=0,nl,write('4L:4 & 3L:0 (Action :Fill 4L jug.)'),XX is 4,water_jug(XX,Y));

(X=:=2,Y=:=0,nl,write('4L:2 & 3L:0 (Action :Goal state reached.)'));

(X=:=4,Y=:=0,nl,write('4L:1 & 3L:3 (Action :pour water from 4L to 3L jug .)'),XX is


X-3,YY is 3,water_jug(XX,YY));

(X=:=0,Y=:=3,nl,write('4L:3 & 3L:0 (Action :pour water from 3L jug to 4L jug.)'),XX


is 3,YY is 0,water_jug(XX,YY));

(X=:=1,Y=:=3,nl,write('4L:1 & 3L:0 (Action :empty 3L jug.)'),YY is


0,water_jug(X,YY));

(X=:=3,Y=:=0,nl,write('4L:3 & 3L:3 (Action :Fill 3L jug.)'),YY is 3,water_jug(X,YY));

(X=:=3,Y=:=3,nl,write('4L:4 & 3L:2 (Action :pour water from 3L jug to 4L jug untill
4L jug is full.)'),XX is X+1,YY is Y-1,water_jug(XX,YY));

(X=:=1,Y=:=0,nl,write('4L:0 & 3L:1 (Action :pour water from 4L to 3L jug.)'),XX is


Y,YY is X ,water_jug(XX,YY));

(X=:=0,Y=:=1,nl,write('4L:4 & 3L:1 (Action :Fill 4L jug.)'),XX is 4,water_jug(XX,Y));

(X=:=4,Y=:=1,nl,write('4L:2 & 3L:3 (Action :pour water from 4L jug to 3L jug untill
3L jug is full.)'),XX is X-2,YY is Y+2,water_jug(XX,YY));

11
(X=:=2,Y=:=3,nl,write('4L:2 & 3L:0 (Action :empty 3L jug.)'),YY is
0,water_jug(X,YY));

(X=:=4,Y=:=2,nl,write('4L:0 & 3L:2 (Action :empty 4L jug.)'),XX is


0,water_jug(XX,Y));

(X=:=0,Y=:=2,nl,write('4L:2 & 3L:0 (Action :pour water from 3L jug to 4L jug.)'),XX


is Y,YY is X,water_jug(XX,YY)).

O/P:-

8.Write simple Prolog functions such as the following. Take into account
lists which are too short. -- remove the Nth item from the list. -- insert as the
Nth item.

% Insert an item as the Nth element in a list

% insert_nth(+Elem, +N, +List, -Result)

12
insert_nth(Elem, 1, List, [Elem|List]) :- !.

insert_nth(Elem, N, [], [Elem]) :- write('List is too short, inserting at the end.'), nl.

insert_nth(Elem, N, [H|T], [H|R]) :-

N > 1,

N1 is N - 1,

insert_nth(Elem, N1, T, R).

% Remove the Nth item from a list

% remove_nth(+N, +List, -Result)

remove_nth(_, [], []) :-

write('List is too short.'), nl.

remove_nth(1, [_|T], T) :- !.

remove_nth(N, [H|T], [H|R]) :-

N > 1,

N1 is N - 1,

remove_nth(N1, T, R).

O/P:-

13
9. Assume the prolog predicate gt(A, B) is true when A is greater than B.
Use this predicate to define the predicate addLeaf(Tree, X, NewTree) which
is true if NewTree is the Tree produced by adding the item X in a leaf node.
Tree and NewTree are binary search trees. The empty tree is represented by
the atom nil.

% Define the gt predicate

gt(A, B) :- A > B.

% Define the addLeaf predicate

addLeaf(nil, X, tree(X, nil, nil)).

addLeaf(tree(Root, Left, Right), X, tree(Root, Left, NewRight)) :-

gt(X, Root),

addLeaf(Right, X, NewRight).

addLeaf(tree(Root, Left, Right), X, tree(Root, NewLeft, Right)) :-

\+ gt(X, Root),

addLeaf(Left, X, NewLeft).

O/P:-

10.Write a Prolog predicate, countLists(Alist, Ne, Nl), using accumulators,


that is true when Nl is the number of items that are listed at the top level of

14
Alist and Ne is the number of empty lists. Suggestion: First try to count the
lists, or empty lists, then modify by adding the other counter.

% Base case: empty list

countLists([], Ne, Nl) :-

countLists([], 0, 0, Ne, Nl).

% Recursive case: non-empty list

countLists([H|T], Ne, Nl) :-

countLists([H|T], 0, 0, Ne, Nl).

% Helper predicate: empty list, end of recursion

countLists([], AccNe, AccNl, AccNe, AccNl).

% Helper predicate: non-empty list with head being an empty list

countLists([[]|T], AccNe, AccNl, Ne, Nl) :-

NewAccNe is AccNe + 1,

NewAccNl is AccNl + 1,

countLists(T, NewAccNe, NewAccNl, Ne, Nl).

% Helper predicate: non-empty list with head being a non-empty list

countLists([H|T], AccNe, AccNl, Ne, Nl) :-

is_list(H),

15
NewAccNl is AccNl + 1,

countLists(T, AccNe, NewAccNl, Ne, Nl).

% Helper predicate: non-empty list with head not being a list

countLists([H|T], AccNe, AccNl, Ne, Nl) :-

\+ is_list(H),

countLists(T, AccNe, AccNl, Ne, Nl).

11. Define a predicate memCount(AList,Blist,Count) that is true if Alist


occurs Count times within Blist. Define without using an accumulator. Use
"not" as defined in utilities.pro, to make similar cases are unique, or else
you may get more than one count as an answer.

memCount(_, [], 0).

memCount(X, [X|T], N) :-

memCount(X, T, N1),

N is N1 + 1.

16
memCount(X, [H|T], N) :-

is_list(H),

memCount(X, H, N1),

memCount(X, T, N2),

N is N1 + N2.

memCount(X, [_|T], N) :- memCount(X, T, N).

member(X, [X|_]).

member(X, [_|Tail]) :- member(X, Tail).

17

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