0

我的任务是:让这个 hanoi 程序在写每个句子之前写一个连续的数字。

河内计划是这样的:

hanoi(N):-move(N,left,middle,right). 
move(0,_,_,_):- !. 
move(N,A,B,C):- M is N-1, move(M,A,C,B), inform(A,B), move(M,C,B,A). 
inform(A,B):- write('MOVING DISK FROM '), write(A),write(' TO '),write(B),nl.

我希望输出看起来像这样:

1: MOVING DISK FROM left TO middle 
2: MOVING DISK FROM left TO right 
3: MOVING DISK FROM middle TO right 
4: MOVING DISK FROM left TO middle 
5: MOVING DISK FROM right TO left 
6: MOVING DISK FROM right TO middle 
7: MOVING DISK FROM left TO middle
4

2 回答 2

1

首先考虑使用 DCG 来描述移动列表:

hanoi(N, Moves) :- phrase(moves(N,left,middle,right), Moves).

moves(0,_,_,_) --> [].
moves(N,A,B,C) --> { N #> 0, M #= N-1 }, moves(M,A,C,B), [A->B], moves(M,C,B,A).

这使您可以将程序逻辑与打印结果等副作用分开。一旦有了动作列表,就很容易编写它们,例如:

write_moves([], _).
write_moves([From->To|Ms], N) :-
        format("~w: move disk from ~w to ~w\n", [N,From,To]),
        N1 #= N + 1,
        write_moves(Ms, N1).

示例查询及其结果:

?- hanoi(3, Moves), write_moves(Moves, 1).
1: move disk from left to middle
2: move disk from left to right
3: move disk from middle to right
4: move disk from left to middle
5: move disk from right to left
6: move disk from right to middle
7: move disk from left to middle
Moves = [ (left->middle), (left->right), (middle->right), ...].
于 2013-10-05T12:30:01.083 回答
0

一种直接的方法是使用表示行号的动态谓词。使用 assert/retract 不是实时友好的,但它适用于这样的应用程序。为了便于阅读,我重新格式化了您的工作 Hanoi 代码,并添加了带有注释的行:

% hanoi
%
:- dynamic(line/1).         % Define a dynamic predicate "line"

hanoi(N) :-
    assertz(line(1)),       % Assert the first line as 1
    move(N, left, middle, right).

move(0, _, _, _) :- !.
move(N, A, B, C) :-
    M is N-1,
    move(M, A, C, B),
    inform(A, B),
    move(M, C, B, A).

inform(A, B) :-
    line(N),                % get the current line number
    NextN is N + 1,         % next line number will be current one plus 1
    retract(line(_)),       % retract the old line number
    assertz(line(NextN)),   % assert the next line number for next time
    write(N),               % write the line number
    write(': '),            %   and a delimiter
    write('MOVING DISK FROM '),
    write(A),
    write(' TO '),
    write(B),
    nl.

还有其他方法可以生成连续数字,例如以下最简单的情况。

sequence(1).
sequence(X) :- sequence(Y), X is Y + 1.

| ?- sequence(X).

X = 1 ? ;

X = 2 ? ;

X = 3 ? ;

X = 4 ?

在某些情况下,可以将类似这样的内容与谓词集成,从而为您提供序列号。由于hanoi谓词具有树状递归,我发现使用assertz机制更容易。

于 2013-10-04T20:46:23.197 回答