2015-11-17 117 views
-1

试图模拟河内游戏单元塔,但我无法正确打印列表。Erlang打印列表

-module(hanoi). 
-export([create_towers/1]). 

create_towers(0) -> 
    []; 

create_towers(X) -> 
List = [X | create_towers(X - 1)], 
List1 = lists:sort(List), 
io:format("Tower1: ~p ~n", [List1]). 

当我运行函数:

67> hanoi:create_towers(3). 
Tower1: [1] 

** exception error: no function clause matching lists:sort([2|ok]) (lists.erl, line 479) 
    in function hanoi:create_towers/1 (hanoi.erl, line 9) 
    in call from hanoi:create_towers/1 (hanoi.erl, line 8) 

回答

2

io:format/2计算为(返回)原子ok所以当你打电话lists:sort(List)你将有一个ok在该列表的末尾。您可能希望有一个功能创建塔和另一个打印他们,因为这些是两个单独的关注。