简体   繁体   English

河内二郎塔

[英]Erlang Towers of Hanoi

Currently stuck on trying to implement the towers of hanoi using a collection. 目前停留在尝试使用集合来实现河内的塔楼。 I am trying to follow the example in Java using stacks http://www.sanfoundry.com/java-program-implement-solve-tower-of-hanoi-using-stacks/ , but I am getting 我正在尝试使用Java使用堆栈http://www.sanfoundry.com/java-program-implement-solve-tower-of-hanoi-using-stacks/的示例,但是我正在理解

-module(toh).
-export([begin/0]).
begin() -> 
  Pegs = 3,
  TowerA = createTowerA(N),
  TowerB = [],
  TowerC = [],
  move(Pegs, TowerA, TowerB, TowerC).

%fills Tower A with integers from Pegs to 1.
createTowerA(0) -> [];
createTowerA(N) when N > 0 ->
   [N] ++ createTowerA(N - 1).

%displays the towers
display(A, B, C) -> 
   io:format("~w\t~w\t~w~n", [A, B, C]).

move(Pegs, TowerA, TowerB, TowerC) -> 
  if Pegs > 0 ->
    move(Pegs, TowerA, TowerC, TowerB),
    Temp = lists:last(TowerA),
    NewTowerC = C ++ Temp,
    NewTowerA = lists:sublist(TowerA, length(TowerA) - 1),
    display(NewTowerA, B, NewTowerC),
    move(Pegs - 1, B, NewTowerA, NewTowerC);
  end

When I try running the code, I get this error. 当我尝试运行代码时,出现此错误。

{"init terminating in do_boot",{undef,[{toh,begin,[],[]},{init,begin_i
t,1,[{file,"init.erl"},{line,1057}]},{init,begin_em,1,[{file,"init.erl"},{line,1
037}]}]}}

Crash dump was written to: erl_crash.dump
init terminating in do_boot ()

Can someone see why this is not working? 有人可以看到为什么这不起作用吗? I'm just trying to follow the sanfoundry example. 我只是想效仿Sanfoundry的例子。

This code cannot compile, at least the variable C in move/4 is unbound when used. 该代码无法编译,至少在使用move / 4时变量C是未绑定的。 So it seems that you didn't compile this file before trying to execute it. 因此,您似乎没有在尝试执行该文件之前对其进行了编译。

Although Erlang used a virtual machine, it must be compiled before execution. 尽管Erlang使用了虚拟机,但必须在执行之前对其进行编译。

There are other problems than the C variable in this code: you call move/4 recursively in the first line of the if, without using any returned value, this cannot have any effect. 除此代码中的C变量外,还有其他问题:如果在不使用任何返回值的情况下,在if的第一行中递归调用move / 4,则不会产生任何效果。 Also you are using a if statement with a bad syntax. 另外,您正在使用语法错误的if语句。 the correct syntax is: 正确的语法是:

if
    GuardSeq1 ->
        Body1;
    ...;
    GuardSeqN ->
        BodyN    % no semicolon at the end of the last body
end

if you intend to use this, beware that you must always have at least one guard that is true, otherwise the code will crash. 如果打算使用此功能,请注意,必须始终至少有一个为真的防护,否则代码将崩溃。

My version [edit: remove useless function, better print]: 我的版本[编辑:删除无用的功能,更好地打印]:

-module (toh).

-export([start/1]).


start(N) ->
    Game = #{1 => lists:seq(1,N), 2 => [], 3 => []},
    display(Game,N),
    move(N,Game,1,3,N).

move(1,Game,From,To,Size) ->
    [H|NewFrom] = maps:get(From,Game),
    NewTo = [H|maps:get(To,Game)],
    NewGame = maps:update(From,NewFrom,maps:update(To,NewTo,Game)),
    display(NewGame,Size),
    NewGame;
move(N,Game,From,To,Size) ->
    Other = other(From,To),
    Game1 = move(N-1,Game,From,Other,Size),
    Game2 = move(1,Game1,From,To,Size),
    move(N-1,Game2,Other,To,Size).

display(#{1 := A, 2 := B, 3 := C},D) ->
    lists:foreach(fun(X) -> print(X,D) end,lists:zip3(complete(A,D),complete(B,D),complete(C,D))),
    io:format("~n~s~n~n",[lists:duplicate(6*D+5,$-)]).

complete(L,D) -> lists:duplicate(D-length(L),0) ++ L.

print({A,B,C},D) -> io:format("~s ~s ~s~n",[elem(A,D),elem(B,D),elem(C,D)]).

elem(I,D) -> lists:duplicate(D-I,$ ) ++ lists:duplicate(I,$_) ++ "|" ++ lists:duplicate(I,$_) ++ lists:duplicate(D-I,$ ).

other(I,J) -> 6-I-J.

In the shell: 在外壳中:

Eshell V6.1  (abort with ^G)
1> c(toh).
{ok,toh}
2> toh:start(3).
  _|_      |       |   
 __|__     |       |   
___|___    |       |   

-----------------------

   |       |       |   
 __|__     |       |   
___|___    |      _|_  

-----------------------

   |       |       |   
   |       |       |   
___|___  __|__    _|_  

-----------------------

   |       |       |   
   |      _|_      |   
___|___  __|__     |   

-----------------------

   |       |       |   
   |      _|_      |   
   |     __|__  ___|___

-----------------------

   |       |       |   
   |       |       |   
  _|_    __|__  ___|___

-----------------------

   |       |       |   
   |       |     __|__ 
  _|_      |    ___|___

-----------------------

   |       |      _|_  
   |       |     __|__ 
   |       |    ___|___

-----------------------

#{1 => [],2 => [],3 => [1,2,3]}
3>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM