Reputation:
I'm trying to create a list in Erlang which will hold a number of integers. So if I pass in 5
it will create a list holding [1,2,3,4,5]
. This is the code I have so far.
So I'd want to call the list something like tower1
-module(towers).
-export([create_towers/1]).
create_towers( 0 ) -> [];
create_towers( N ) when N > 0 -> create_towers( N-1 ) ++ [N].
Upvotes: 2
Views: 102
Reputation: 9693
The code is good, the problem is the syntax
-module(towers).
-export([create_towers/1]).
create_towers(0) ->
[];
create_towers(N) when N > 0 ->
create_towers(N-1) ++ [N].
works fine
Eshell V7.1 (abort with ^G)
(emacs@Mac-mini-de-Rodrigo)1> c("/Users/rorra/erlang/towers", [{outdir, "/Users/rorra/erlang/"}]).
{ok,towers}
(emacs@Mac-mini-de-Rodrigo)2> towers:create_towers(0).
[]
(emacs@Mac-mini-de-Rodrigo)3> towers:create_towers(10).
[1,2,3,4,5,6,7,8,9,10]
If you want it to call the list created tower1:
(emacs@Mac-mini-de-Rodrigo)2> Tower1 = towers:create_towers(5).
[1,2,3,4,5]
notice that all variables starts with upperase, if you want to name the module towers1, change the file name to tower1.erl and add:
-module(tower1).
-export([create_towers/1]).
create_towers(0) ->
[];
create_towers(N) when N > 0 ->
create_towers(N-1) ++ [N].
and then you can call towers1:create_towers(N) like:
(emacs@Mac-mini-de-Rodrigo)2> MyVar = tower1:create_towers(5).
[1,2,3,4,5]
Upvotes: 1