Prolog Record
Prolog Record
(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.
1
LIST OF PROGRAMS
4 Medical Diagnosis
5 4 - Queen problem
2
1. Write simple fact for following:
likes(ram,mango).
likes(bill,cindy).
red(rose).
own(john,gold).
girl(seema).
O/P:-
3
temp:- write('enter temperature in celsius:'),nl,
read(T),FT is (9/5*T)+32,write(FT).
4
canget(State1, CurrentActions, Actions) :-
move(State1, Action, State2),
\+ member(Action, CurrentActions), % Avoid loops
canget(State2, [Action | CurrentActions], Actions).
4. WAP in turbo prolog for medical diagnosis and show the advantages
and disadvantages of green and red 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.
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.
6
symptom(P,fatigue):-write("Does "),write(P),write(" has
fatigue(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),
queens(Board, 0, Queens).
Col is Col0+1,
functor(Vars, f, N),
8
board(Queens, Board, Col, N, VR, [_|VC]).
constraints(0, _, _, _) :- !.
M is N-1,
queens([], _, []).
Row is Row0+1,
O/P:-
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.
(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=:=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));
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.
12
insert_nth(Elem, 1, List, [Elem|List]) :- !.
insert_nth(Elem, N, [], [Elem]) :- write('List is too short, inserting at the end.'), nl.
N > 1,
N1 is N - 1,
remove_nth(1, [_|T], T) :- !.
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.
gt(A, B) :- A > B.
gt(X, Root),
addLeaf(Right, X, NewRight).
\+ gt(X, Root),
addLeaf(Left, X, NewLeft).
O/P:-
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.
NewAccNe is AccNe + 1,
NewAccNl is AccNl + 1,
is_list(H),
15
NewAccNl is AccNl + 1,
\+ is_list(H),
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.
member(X, [X|_]).
17