Nihir
Nihir

Reputation: 374

Erlang printing lists

Trying to simulate a Towers of Hanoi game for uni, but I can't get the lists to print properly.

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

When I run the function:

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)

Upvotes: 0

Views: 1472

Answers (1)

Emil Vikström
Emil Vikström

Reputation: 91963

io:format/2 evaluates to (returns) the atom ok so when you call lists:sort(List) you will have an ok at the end of that list. You might want to have one function to create towers and another to print them, because those are two separate concerns.

Upvotes: 3

Related Questions