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

Oliver Korpilla Oliver.Korpilla@REDACTED
Mon Apr 25 00:06:26 CEST 2016


Hello, Donald.

Like Lisp Erlang is not very performant for joining elements to the end of a list. Adding things to the front is simple, cheap, and has its own syntax:

List2 = [ Item | List1].

If you accumulate your list in the wrong order, a highly optimized list:reverse/1 function is available: http://erlang.org/doc/man/lists.html#reverse-1

Also, most semantics you might have in mind would also work for lists with newest entry in front.

What you describe below is a problem in any language as long as you don't use some sort of looping construct. 

A simple process that just waits for new events to learn about might look like this:

-module(example).
-export([start/0]).

start() -> spawn(fun() -> receive_events_loop([]) end).

receive_events_loop(EventList) ->
  receive
    {query, From} -> 
      From ! {reply, lists:reverse(EventList) };
    {add, Entry} ->
      receive_events_loop([ Entry | EventList ])
  end.

If you start this process, it will loop forever, waiting quietly for events.

You would work with it like this:

Pid = example:start().
Pid ! {add, hello}.
Pid ! {add, world}.
Pid ! {query, self()}.

You would see that this process does accumulate a list internally while running through its receive loop repeatedly. Nowhere do you need variables of the kind List1, List2, List3, ... You run a loop, but this loop is implemented through recursion.

Understanding recursion, list accumulation, and especially tail recursion for implementing loops is key to programming Erlang.

(I did not compile my example. I just wrote it for the email. Excuse any mistakes I made.)

Hope this helped,
Oliver
 

Gesendet: Sonntag, 24. April 2016 um 23:24 Uhr
Von: "Donald Steven" <t6sn7gt@REDACTED>
An: "Oliver Korpilla" <Oliver.Korpilla@REDACTED>
Cc: "Antonios Kouzoupis" <kouzan@REDACTED>, erlang-questions@REDACTED
Betreff: Re: Aw: Re: [erlang-questions] How to keep adding items to a data structure
Thanks Oliver, I appreciate your note. I understand that a list can
contain different elements, as you've indicated. That's wonderful! And
I have that book on order. What I'm so frustrated by is, apparently, in
order to extend a list by adding new musical events, that I have to keep
creating new lists, like:

List1 = [musical-event],
List2 = List1 ++ [new-musical-event],
List3 = List2 ++ [new-musical-event],
List4 = List3 ++ [new-musical-event],
etc.

all the way to perhaps a few thousand. This is primitive and absurd. I
need a better solution, whether it's a list or an array or a record.

Don

On 04/24/2016 05:05 PM, Oliver Korpilla wrote:
> Hello, Donald.
>
> Lists are not fixed to one type like arrays in C are.
>
> [[1,2,3],"hello",5,world] is a valid Erlang expression, even though the list contains another list, a string, a number, and an atom.
>
> You might want to read a book like "Programming Erlang" to get into the language. I highly recommend it, it really helped laying down the basics and understand the design principles of the OTP shipped with Erlang.
>
> Cheers,
> Oliver
>
>
> Gesendet: Sonntag, 24. April 2016 um 22:05 Uhr
> Von: "Donald Steven" <t6sn7gt@REDACTED>
> An: "Antonios Kouzoupis" <kouzan@REDACTED>, erlang-questions@REDACTED
> Betreff: Re: [erlang-questions] How to keep adding items to a data structure
> Hi Antonios,
>
> You are kind and generous to help. I'll study this carefully. I will
> be wanting to add a list or a record, not just numbers. Is this possible?
>
> Don
>
> On 04/24/2016 03:13 PM, Antonios Kouzoupis wrote:
>> 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) ->
>> Acc;
>> populate(Num, Acc) ->
>> populate(Num - 1, [Num | Acc]).
>>
>>
>> Now if you call populate(100), you'll get the list [1,...,100]
>>
>> BR,
>>
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions[http://erlang.org/mailman/listinfo/erlang-questions][http://erlang.org/mailman/listinfo/erlang-questions[http://erlang.org/mailman/listinfo/erlang-questions]]
 



More information about the erlang-questions mailing list