[erlang-questions] How to keep adding items to a data structure

Antonios Kouzoupis kouzan@REDACTED
Sun Apr 24 21:13:55 CEST 2016

Hi Don,

The way you iterate in Erlang and I guess in most functional programming
languages is by recursive call. So if you want to add/append some
numbers to a list, one way to go is the following:

populate(Num) ->
    populate(Num, []).

populate(0, Acc) ->
populate(Num, Acc) ->
    populate(Num - 1, [Num | Acc]).

Now if you call populate(100), you'll get the list [1,...,100]


Αντώνης Κουζούπης
Antonios Kouzoupis

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: OpenPGP digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160424/f2457df9/attachment.bin>

More information about the erlang-questions mailing list