-6

for Towers of Hanoi, the output should look like this:

 ?-move(3,[1,2,3],[],[],A1,B1,C).

 A1=[].
 B1=[1,2,3]
 C=[].

I know how to do this:

move(1,[H|T],B,C,A1,B1,C) :-  
                       A1 = T,
                       B1 = [H|B],
                       C1 = C.  
move(N,A,B,C,A1,B1,C) :- 
                       N>1, 
                       M is N-1, 
                       move(M,[H|T],C,B,A1,B1,C), 
                       move(1,[H|T],B,_,A1,B1,C), 
                       move(M,C,B,[H|T],A1,B1,C).

But the next question is: "rewrite the program to provide illustrations of towers after every move". Its output should look like this:

?- move(3,tower(a,[1,2,3]),tower(b,[]),tower(c,[]),A,B,C).
   a[2 3] b[1] C[]
   a[3] b[1] C[2]
   a[3] b[] C[1 2]
   a[] b[3] C[1 2]
   a[1] b[3] C[2]
   a[1] b[2 3] C[]
   a[] b[1 2 3] C[]
   A = tower(a, [])
   B = tower(b,[1, 2, 3])
   C = tower(c,[])

How can this be done?

false
  • 10,264
  • 13
  • 101
  • 209
user1400451
  • 87
  • 1
  • 8

1 Answers1

1

You can use the following predicate to print a space separated list:

write_list_([]) :- write(']').
write_list_([X|Xs]) :- write(' '), write(X), write_list_(Xs).

write_list([]) :- write('[]').
write_list([X|Xs]) :- write('['), write(X), write_list_(Xs).
Alexander Serebrenik
  • 3,567
  • 2
  • 16
  • 32
  • i tried but i still stuck in there cant get out. not much time for me, exam in monday afternoo. really need help – user1400451 May 26 '12 at 13:06