I'm trying to do the Towers of Hanoi problem, what I have tried so far:
move(1,[H|T],B,C,A1,B1,C) :-
A1 = T,
B1 = [H|B].
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 this code does not work, I need get the result is looks like this:
?-move(3,[1,2,3],[],[],A1,B1,C).
and the results:
A1=[].
B1=[1,2,3]
C=[].
can someone help me fix my code up and can get the result like that? This is very important for me, I really need help.
this is what i did but with some problems:
move(N,[H|T],[],[],A1,B1,C) :-
N > 1,
M is N - 1,
move(N,[H|M],[H|_],[],A1,B1,C),
move(M,[_|M],[H|_],[H|_],A1,B1,C),
move(M,[_|M],[],[H|T],A1,B1,C),
move(M,[],[_|T],[H|T],A1,B1,C),
move(M,[H|_],[_|T],[H|T],A1,B1,C),
move(M,[H|_],[_|T],[],A1,B1,C),
move(M,[],[H|T],[],A1,B1,C).
move(N,[H|T],[],[]) :- write(A1), nl,
write(B1), nl,
write(C).
See Question&Answers more detail:
os