0% found this document useful (0 votes)
27 views

Prolog Assignment - 2

The document contains solutions to 5 problems involving list processing in Prolog: 1) Defining a remove_at/3 predicate to remove an element from a specific position in a list, 2) Defining a replace_at/4 predicate to replace an element at a specific position in a list, 3) Defining a merge_lists/3 predicate to merge two lists by alternating elements, 4) Defining an even_elements/2 predicate to extract the even-indexed elements from a list, and 5) Defining an is_palindrome/1 predicate to check if a list is a palindrome. Each problem includes the question, solution definition, and example test case.

Uploaded by

melkter3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Prolog Assignment - 2

The document contains solutions to 5 problems involving list processing in Prolog: 1) Defining a remove_at/3 predicate to remove an element from a specific position in a list, 2) Defining a replace_at/4 predicate to replace an element at a specific position in a list, 3) Defining a merge_lists/3 predicate to merge two lists by alternating elements, 4) Defining an even_elements/2 predicate to extract the even-indexed elements from a list, and 5) Defining an is_palindrome/1 predicate to check if a list is a palindrome. Each problem includes the question, solution definition, and example test case.

Uploaded by

melkter3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Question 1

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').

Trace result and description of each trace output


The screenshot doesn’t include all the traces because because of recursion the result is very
long.

```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'.

This pattern repeats for subsequent calls:

```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

b. Define a predicate called replace_at(Pos,X,L1,L2) that


replaces the element at position Pos of L1 by X and returns
L2.
? – replace_at(2, x, [a,b,c,d],L). L=[a,x,c,d].

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

d. Define a predicate called even_elements(L1,L2) that extracts


all even-indexed elements of L1 to L2 and returns L2.
?- even_elements([a,b,c,d,e,f],L). L=[b,d,f]

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-

You might also like