Prolog Assignment - 2
Prolog Assignment - 2
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.
Solution
move(1,X,Y,_):-
write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z):-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
Testing the code:
?- move(5, 'Source', 'Destination', 'Auxiliary').
```prolog
[trace] ?- move(5, 'Source', 'Destination', 'Auxiliary').
Call: (10) move(5, 'Source', 'Destination', 'Auxiliary') ? creep
```
- This is the initial query, attempting to move 5 disks from the 'Source' peg to the 'Destination'
peg using the 'Auxiliary' peg.
```prolog
Call: (11) 5>1 ? creep
Exit: (11) 5>1 ? creep
```
- Checks if the number of disks (5) is greater than 1, which is true. Proceeds to the next step.
```prolog
Call: (11) _30762 is 5+ -1 ? creep
Exit: (11) 4 is 5+ -1 ? creep
```
- Subtracts 1 from the number of disks, resulting in 4.
```prolog
Call: (11) move(4, 'Source', 'Auxiliary', 'Destination') ? creep
```
- Initiates a recursive call to move 4 disks from 'Source' to 'Auxiliary' using 'Destination'.
```prolog
Call: (12) 4>1 ? creep
Exit: (12) 4>1 ? creep
Call: (12) _34828 is 4+ -1 ? creep
Exit: (12) 3 is 4+ -1 ? creep
Call: (12) move(3, 'Source', 'Destination', 'Auxiliary') ? creep
```
```prolog
Call: (13) 3>1 ? creep
Exit: (13) 3>1 ? creep
Call: (13) _38894 is 3+ -1 ? creep
Exit: (13) 2 is 3+ -1 ? creep
Call: (13) move(2, 'Source', 'Auxiliary', 'Destination') ? creep
```
```prolog
Call: (14) 2>1 ? creep
Exit: (14) 2>1 ? creep
Call: (14) _42960 is 2+ -1 ? creep
Exit: (14) 1 is 2+ -1 ? creep
Call: (14) move(1, 'Source', 'Destination', 'Auxiliary') ? creep
```
At this point, the recursion reaches the base case (moving 1 disk), and the following lines
describe the move:
```prolog
Call: (15) write('Move top disk from ') ? creep
Move top disk from
Exit: (15) write('Move top disk from ') ? creep
Call: (15) write('Source') ? creep
Source
Exit: (15) write('Source') ? creep
Call: (15) write(' to ') ? creep
to
Exit: (15) write(' to ') ? creep
Call: (15) write('Destination') ? creep
Destination
Exit: (15) write('Destination') ? creep
Call: (15) nl ? creep
```
- This prints a message indicating the move of the top disk from 'Source' to 'Destination'.
The process continues with similar recursive calls and messages until the entire Towers of
Hanoi problem is solved, and the final result is:
```prolog
true.
```
This `true` indicates that the original query `move(5, 'Source', 'Destination', 'Auxiliary')` has been
successfully satisfied. The output, when read from top to bottom, represents the sequence of
moves needed to solve the Towers of Hanoi problem with 5 disks.
Question 2
a. Define a predicate called remove_at(Pos,L1,L2) that
removes an element from a specific position Pos of the list
L1 and returns L2.
? – remove_at(3,[a,b,c,d],L). L=[a,b,d].
Answer-a
Test-a
Answer-b
Test-b
c. Define a predicate called merge_lists(L1,L2,L3) that merges
L1 and L2 by alternating elements from each and returns L3.
?- merge_lists([a,b,c],[1,2,3],L). L=[a,1,b,2,c,3]
Answer-c
Test-c
Answer-d
Test-d
e. Define a predicate called is_palindrome(L) that checks if the
list L is palindrome.
?- is_palindrome([1,2,3,2,1]). true.
Answer-e
Test-