AI Remaining
AI Remaining
AI Remaining
Code:
% Base case: Delete 1st position
delete_nth(1, [_|T], T).
Output:
12. Write a Prolog program for BFS (Breadth First Search) .
Code:
% Define the graph as a list of edges
edge(a, b).
edge(a, c).
edge(b, d).
edge(c, e).
edge(d, f).
edge(e, f).
% BFS predicate
bfs(Start, Goal, Path) :- bfs([Start], [Start], Goal, Path).
% Helper predicate
bfs([], _, _, _) :- fail.
bfs([Node|Nodes], Visited, Goal, Path) :-
( Node = Goal
-> reverse(Visited, Path)
; findall(NewNode, (edge(Node, NewNode), \+ member(NewNode,
Visited)), NewNodes),
append(Nodes, NewNodes, NextNodes),
bfs(NextNodes, [Node|Visited], Goal, Path)
).
Output:
13. Write a Prolog program for DFS (Depth First Search) .
Code:
% Define the graph as a list of edges
edge(a, b).
edge(a, c).
edge(b, d).
edge(c, e).
edge(d, f).
edge(e, f).
% DFS predicate
dfs(Start, Goal, Path) :- dfs([Start], Start, Goal, Path).
% Helper predicate
dfs(Visited, Node, Node, [Node|Visited]).
dfs(Visited, Start, Goal, Path) :-
edge(Start, Next),
\+ member(Next, Visited),
dfs([Next|Visited], Next, Goal, Path).
Output:
14. Write a Prolog program for Family Tree.
Code:
%% Define family relationships
father(john, emily).
father(john, michael).
mother(mary, emily).
mother(mary, michael).
father(david, sarah).
mother(emily, sarah).
father(michael, oliver).
mother(sarah, oliver).
%% Define gender
male(john).
male(michael).
male(david).
male(oliver).
female(mary).
female(emily).
female(sarah).
Output:
15. Write a Prolog program for Calculator .
Code:
%% Arithmetic Operations
%% Trigonometric Operations
Code:
%% List Operations
%% Append
append([], L, L).
append([H|T], L, [H|R]) :- append(T, L, R).
%% Length
length([], 0).
length([_|T], N) :- length(T, N1), N is N1 + 1.
%% Reverse
reverse(L, R) :- reverse(L, [], R).
reverse([], R, R).
reverse([H|T], Acc, R) :- reverse(T, [H|Acc], R).
%% Member
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
%% Delete
delete([], _, []).
delete([X|T], X, T).
delete([H|T], X, [H|R]) :- delete(T, X, R).
%% Insert
insert(X, [], [X]).
insert(X, [H|T], [X, H|T]).
insert(X, [H|T], [H|R]) :- insert(X, T, R).
%% Sort
sort([], []).
sort([X|T], S) :-
sort(T, S1),
insert(X, S1, S).
%% Maximum
max([X], X).
max([X|T], M) :-
max(T, M1),
M is max(X, M1).
%% Minimum
min([X], X).
min([X|T], M) :-
min(T, M1),
M is min(X, M1).
%% Sum
sum([], 0).
sum([H|T], S) :-
sum(T, S1),
S is H + S1.
Output: