Ai Practicals
Ai Practicals
Theory:
Prolog as the name itself suggests, is the short form of LOGical PROgramming.
Prolog is a logical and declarative programming language. It is one major
example of the fourth-generation language that supports the declarative
programming paradigm. This is particularly suitable for programs that
involve symbolic or non-numeric computation. This is the main reason to use
Prolog as the programming language in Artificial Intelligence, where symbol
manipulation and inference manipulation are the fundamental tasks.
In Prolog, we need not mention the way how one problem can be solved, we just
need to mention what the problem is, so that Prolog automatically solves it.
However, in Prolog we are supposed to give clues as the solution method.
Prolog language basically has three different elements –
1. Facts − The fact is predicate that is true, for example, if we say, “Tom is
the son of Jack”, then this is a fact.
2. Rules − Rules are extinctions of facts that contain conditional clauses. To
satisfy a rule these conditions should be met.
3. Questions − And to run a prolog program, we need some questions, and
those questions can be answered by the given facts and rules.
Installation Guide:
• You can verify the below screen, and check/uncheck appropriate boxes,
otherwise you can leave it as default. Then click on next.
• In the next step, you will see the below screen, then click on Install.
Input:
has_symptom(Disease, Symptom) :-
symptoms(Disease, SymptomList),
member(Symptom, SymptomList).
list_symptoms(Disease) :-
symptoms(Disease, SymptomList),
format('The symptoms of ~w are: ~w~n', [Disease, SymptomList]).
find_disease(Symptom) :-
symptoms(Disease, SymptomList),
member(Symptom, SymptomList),
format('The disease with symptom ~w is ~w.~n',[Symptom, Disease]).
Output:
Input:
factorial(0,1).
factorial(N,Answer) :-
N>0,
N1 is N-1,
factorial(N1, M1),
Answer is N*M1.
Output:
PRACTICAL -4
For Appending:
Input:
Output:
• append_list([a,b,c],[p,q], Ans).
Ans = [a,b,c,p,q]
• append_list([1,2,7],[3,14,85], Ans).
Ans = [1,2,7,3,14,85]
• append_list([1,2,3],[p,q], Ans).
Ans = [1,2,3,p,q]
• append_list([7,8,a,10],[p,q], Ans).
Ans = [7,8,a,10,p,q]
For Reversing:
Input:
list_concat([],L,L).
list_concat([X1|L1],L2,[X1|L3]) :-
list_concat(L1,L2,L3).
list_rev([],[]).
list_rev([Head|Tail],Reversed) :-
list_rev(Tail, RevTail),
list_concat(RevTail, [Head],Reversed).
Output:
• list_rev([1,2,3,7,8],ReverseList).
ReverseList = [8,7,3,2,1]
• list_rev([1,2,3,7,8],Answer).
Answer = [8,7,3,2,1]
• list_rev([6,8,9,74,5],NewList).
NewList = [5,74,9,8,6]
PRACTICAL -5
Input:
sum_list([], 0).
sum_list([H|T], Sum) :-
sum_list(T, Rest),
Sum is H + Rest.
Output:
PRACTICAL -6
Input:
start :-
Board = ['_', '_', '_', '_', '_', '_', '_', '_', '_'],
display_board(Board),
play(Board, 'x').
play(Board, Player) :-
read(Position),
display_board(NewBoard),
; draw(NewBoard) ->
write('It\'s a draw!~n')
; switch_player(Player, NextPlayer),
play(NewBoard, NextPlayer))
play(Board, Player)).
% Switch players
switch_player('x', 'o').
switch_player('o', 'x').
valid_move(Board, Position) :-
% Make a move
replace([_|T], 1, X, [X|T]).
win(Board, Player) :-
row([X,X,X,_,_,_,_,_,_], X).
row([_,_,_,X,X,X,_,_,_], X).
row([_,_,_,_,_,_,X,X,X], X).
col([X,_,_,X,_,_,X,_,_], X).
col([_,X,_,_,X,_,_,X,_], X).
col([_,_,X,_,_,X,_,_,X], X).
diag([X,_,_,_,X,_,_,_,X], X).
diag([_,_,X,_,X,_,X,_,_], X).
display_board([A, B, C, D, E, F, G, H, I]) :-
write('---------'), nl,
format('~w | ~w | ~w~n', [D, E, F]),
write('---------'), nl,
Output:
PRACTICAL -7
Input:(Using DFS)
% Depth-first search
dfs(State, Visited, Path) :-
is_goal(State),
reverse(Path, FinalPath),
write('Solution Path: '), write(FinalPath), nl.
Input:(Using BFS)
% BFS function
bfs([[X, Y] | _], _, [[X, Y]]) :-
target(Target), (X = Target; Y = Target). % Goal state: one jug has the target
amount
Input:
tower_of_hanoi(0, _, _, _) :- !.
N1 is N - 1,
move_disk(Source, Target) :-
Input: