【问题标题】:Erlang printing listsErlang 打印列表
【发布时间】:2016-02-19 02:58:09
【问题描述】:

尝试为 uni 模拟河内塔游戏,但无法正确打印列表。

-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)

【问题讨论】:

    标签: functional-programming erlang


    【解决方案1】:

    io:format/2 计算为(返回)原子ok,因此当您调用lists:sort(List) 时,该列表末尾将有一个ok。您可能希望有一个函数来创建塔,而另一个函数来打印它们,因为这是两个不同的问题。

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多