21hcs4108 Akansha-Nep
21hcs4108 Akansha-Nep
21hcs4108 Akansha-Nep
move(N,X,Y,Z):-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,Z).
tower_of_hanoi(N):-
move(N,'A','C','B').
2. Write a program to implement the Hill climbing search algorithm in Prolog.
hill_climb(List, Max):-
select_max(List, Max, Rest),
hill_climb(Rest, Max, Max).
% Base case: When the final state is reached, return the path
astar1([(_, _, Tp, [Final | R]) | _], Final, [Final | R], Tp) :-
reverse([Final | R], Path),
write('Path = '), write(Path).
street(1, 2, 50).
street(1, 3, 100).
street(1, 4, 75).
street(1, 5, 65).
street(2, 5, 30).
street(3, 4, 40).
street(3, 7, 50).
street(4, 5, 25).
street(4, 6, 30).
street(4, 9, 65).
street(4, 7, 70).
street(5, 6, 30).
street(6, 9, 35).
street(6, 10, 60).
street(7, 8, 35).
street(7, 9, 30).
street(8, 9, 70).
street(8, 11, 40).
street(8, 13, 50).
street(9, 10, 50).
street(9, 11, 30).
street(10, 11, 50).
street(10, 12, 40).
street(11, 12, 40).
street(11, 13, 50).
street(11, 14, 60).
street(12, 14, 50).
street(13, 14, 50).
% Define other streets as needed
print_row([]):- nl.
print_row([Cell|Row]):-
format('~d', [Cell]),
write(' '),
(Row = [] -> true ; write(',')),
print_row(Row).
% Example Sudoku grid
example_sudoku([
[_, _, _, 2, 6, _, 7, _, 1],
[6, 8, _, _, 7, _, _, 9, _],
[1, 9, _, _, _, 4, 5, _, _],
[8, 2, _, 1, _, _, _, 4, _],
[_, _, 4, 6, _, 2, 9, _, _],
[_, 5, _, _, _, 3, _, 2, 8],
[_, _, 9, 3, _, _, _, 7, 4],
[_, 4, _, _, 5, _, _, 3, 6],
[7, _, 3, _, 1, 8, _, _, _]
]).
solve_and_print_sudoku:-
example_sudoku(Grid),
sudoku(Grid),
maplist(labeling([ff]), Grid),
print_sudoku(Grid).
8. Write a Prolog program to implement the family tree and demonstrate the
family relationship.
male(joy).
male(sam).
male(john).
male(bob).
male(jane).
female(lisa).
female(mary).
female(susan).
female(anisa).
parent(sam,joy).
parent(sam,lisa).
parent(mary,joy).
parent(mary,lisa).
parent(bob,jane).
parent(anisa,jane).
parent(john,sam).
parent(susan,sam).
parent(john,bob).
parent(susan,bob).
father(F,C):-
male(F),
parent(F,C).
mother(M,C):-
female(M),
parent(M,C).
sibling(S1,S2):-
parent(P,S1),
parent(P,S2),
S1\=S2.
brother(B,P):-
male(B),
sibling(B,P).
sister(S,P):-
female(S),
sibling(S,P).
grandparent(Gp,Gc):-
parent(Gp,P),
parent(P,Gc).
wife(W,H):-
female(W),
parent(H,C),
parent(W,C),
H\=W.
husband(H,W):-
male(H),
parent(H,C),
parent(W,C),
H\=W.
cousin(P1,P2):-
parent(Parent1,P1),
parent(Parent2,P2),
sibling(Parent1,Parent2),
P1\=P2.
frame(jane, [
[name, 'Jane Smith'],
[age, 28],
[gender, female],
[occupation, doctor]
]).
frame(car1, [
[make, toyota],
[model, 'Corolla'],
[year, 2018],
[color, blue]
]).
frame(car2, [
[make, honda],
[model, 'Civic'],
[year, 2020],
[color, red]
]).
% Example queries:
% Get the name of John
% ?- get_frame_property(john, name, Name).
%
% Get the color of car2
% ?- get_frame_property(car2, color, Color).
10. Write a Prolog Program to implement conc(L1,L2,L3) where appended with
L1 to get the resulted L3.
conc([], L, L).
conc([X1|L1], L2, [X1|L3]) :-
append(L1,L2,L3).
accReverse([H|T],A,R):-
accReverse(T,[H|A],R).
accReverse([],A,A).
12. Write a Prolog program to generate a parse tree of given sentences
assuming the grammar required for parsing.
% Defining the grammar rules.
sentence(Tree) --> noun_phrase(NP), verb_phrase(VP), {Tree = [NP,
VP]}.
noun_phrase(Tree) --> determiner(Det), noun(N), {Tree = np(Det, N)}.
verb_phrase(Tree) --> verb(V), noun_phrase(NP), {Tree = vp(V, NP)}.
% Lexical rules.
determiner(the) --> [the].
determiner(a) --> [a].
noun(cat) --> [cat].
noun(dog) --> [dog].
verb(chased) --> [chased].
verb(ate) --> [ate].