I need a step by step explanation of how prolog calls and ex

I need a step by step explanation of how prolog calls and executes the following query:

moves(3,\'Pega\', \'Pegc\', \'Pegb\').

Database:

moves(1,X,Y,_):-
   write(\'Move disc from \'),
   write(X),
   write(\' to \'),
   write(Y),
   nl.

%Left, Right, Center
moves(N,L,R,C):-
   M is N-1,
   moves(M,L,C,R),
   moves(1,L,R,_),
   moves(M,C,R,L).

Solution

Here is the explanation for you:

Initially, the call is: moves(3, \'Pega\', \'Pegc\', \'Pegb\')
And when it is called, the recursive call is:
M is now 2.
moves(2, Pega, Pegb, Pegc) is called.
Then again moves(1, Pega, Pegc, Pegb) is called.
Then the string to be printed is: Move disc from Pega to Pegc.
Then again moves(1, Pega, Pegb, _) is called.
Then the string to be printed is: Move disc from Pega to Pegb.
Then again moves(1, Pegc, Pegb, Pega) is called.
Then the string to be printed is: Move disc from Pegc to Pegb.
Then again moves(1, Pega, Pegc, _) is called.
Then the string to be printed is: Moved disc from Pega to Pegc.
Then again moves(1, Pegc, Pega, Pegb) is called.
Then the string to be printed is: Moved disc from Pegb to Pega.
Then again moves(1, Pegb, Pegc, _) is called.
Then the string to be printed is: Moved disc from Pegb to Pegc.
Then again moves(1, Pega, Pegc, _) is called.
Then the string to be printed is: Moved disc from Pega to Pegc.

I need a step by step explanation of how prolog calls and executes the following query: moves(3,\'Pega\', \'Pegc\', \'Pegb\'). Database: moves(1,X,Y,_):- write(

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site