Professional Documents
Culture Documents
AI LAB ASSIGNME-WPS Office
AI LAB ASSIGNME-WPS Office
AI LAB ASSIGNME-WPS Office
AI LAB ASSIGNMENT
1. Provide the solution for the towers of Hanoi problem; give the complete trace for the
program to move 5 disks from the source to the destination with explanation of each
line of the trace in the same way as the trace of the monkey and banana problem is
done above.
The following defines the hanoi predicate for moving N disks from Source to
Now, let's go through the trace for moving 5 disks from the source rod to the
destination rod:
?- move_tower.
true .
1. ?- move_tower.
This query seems to call a predicate or rule named move_tower. Let's assume that
move_tower is a Prolog rule for solving the Tower of Hanoi problem. Here's an
This represents the initial move of the smallest disk from rod 'A' to rod 'C'.
The program recursively solves the Tower of Hanoi problem for N-1 disks (N =
This results in the move of the second disk from 'A' to 'B'.
The program completes the move for the smallest disk from 'C' to 'B'.
The program recursively solves the Tower of Hanoi problem for N-1 disks (N =
This results in the move of the third disk from 'A' to 'C'.
The program completes the move for the smallest disk from 'B' to 'A'.
The program recursively solves the Tower of Hanoi problem for N-1 disks (N =
This results in the move of the second disk from 'B' to 'C'.
8. Move disk 1 from A to C:
The program completes the move for the smallest disk from 'A' to 'C'.
The program recursively solves the Tower of Hanoi problem for N-1 disks (N =
This results in the move of the fourth disk from 'A' to 'B'.
...and so on.
The trace continues similarly, and each line represents a move in the Tower of Hanoi solution.
The sequence of moves follows the classical recursive approach to solving the Tower of Hanoi
problem.
N > 1,
M is N - 1,
% Move n-1 disks from source to auxiliary using target as an auxiliary peg
% Move the n-1 disks from auxiliary to target using source as an auxiliary peg
?- hanoi(4,'A','C','B',Actions).
Actions = [move(1, 'A', 'B'), move(2, 'A', 'C'), move(1, 'B', 'C'), move(3, 'A', 'B'), move(1, 'C', 'A'),
move(2, 'C', 'B'), move(1, 'A', 'B'), move(4, 'A', 'C'), move(..., ..., ...)|...]
1. hanoi(4,'A','C','B',Actions).
It is a query that is generating a sequence of moves for the Tower of Hanoi problem with
4 disks, moving from rod 'A' to rod 'C' using rod 'B' as an auxiliary peg.
The program starts by moving the smallest disk from rod 'A' to rod 'B'.
The program recursively solves the Tower of Hanoi problem for N-1 disks (N = 3) from
The program completes the move for the smallest disk from 'B' to 'C'.
The program recursively solves the Tower of Hanoi problem for N-1 disks (N = 3) from
The program completes the move for the smallest disk from 'C' to 'A'.
The program recursively solves the Tower of Hanoi problem for N-1 disks (N = 3) from
The program completes the move for the smallest disk from 'A' to 'B'.
9. Move disk 4 from 'A' to 'C':
The program recursively solves the Tower of Hanoi problem for N-1 disks (N = 3) from
The trace continues similarly for the remaining steps, moving the disks according to the rules of the
Tower of Hanoi.
a) Define a predicate called remove_at(Pos,L1,L2) that removes an element from a specific position
If Pos is greater than 1, recursively remove the element at the specified position in the tail
of the list.
Querying this will unify L with the list [a, b, d], which is the result of removing the
b) Define a predicate called replace_at(Pos,X,L1,L2) that replaces the element at position Pos of L1
Replacing the element at position 1 results in the new element X being placed at the head
of the list.
Pos > 1,
NextPos is Pos - 1,
If ‘Pos’ is greater than 1, recursively replace the element at the specified position in the
Querying this will unify L with the list [a, x, c, d], which is the result of replacing the
When you run the example query, Prolog will respond with L = [a, x, c, d].
c) Define a predicate called merge_lists(L1,L2,L3) that merges L1 and L2 by alternating elements
1. merge_lists([], L, L).
Merging an empty list with another list results in the second list.
Similarly, merging a non-empty list with an empty list results in the first list.
Merge two non-empty lists by alternating elements, placing X and Y in the merged list.
Querying this will unify L with the list [a, 1, b, 2, c, 3], which is the result of merging
When you run the example query, Prolog will respond with L = [a, 1, b, 2, c, 3].
2. even_elements([_], []).
even_elements(Tail, ResultTail)
Extract even-indexed elements by taking the second element (E) and then recursively
Querying this will unify L with the list [b, d, f], which is the result of extracting the
When you run the example query, Prolog will respond with L = [b, d, f].
?- is_palindrome([1,2,3,2,1]). true.
2. is_palindrome([Head|Tail]) :-
reverse_list(Tail, ReversedTail),
Check if the list is a palindrome by comparing the head with the reversed tail.
3. reverse_list([], []).
This rule states that an empty list [] is already reversed as an empty list []. It serves as
4. reverse_list([Head|Tail], Reversed) :-
reverse_list(Tail, ReversedTail),
This rule is used to reverse a non-empty list [Head|Tail]. It uses recursion and the
reverse_list(Tail, ReversedTail): This recursively reverses the tail of the list (Tail)
(ReversedTail) with the head of the original list [Head] to get the reversed list
Reversed.
Therefore, the query ?- is_palindrome([1, 2, 3, 2, 1]). is true, indicating that the list