[erlang-questions] Learning erlang - is this an efficient way to build a list?
Fri Oct 12 22:57:01 CEST 2007
In general what I've seen is to use the cons operator rather than append,
because append is O(n) and cons is O(1), but in your case it's fine as you
are still reversing the list, and hence your append is O(1) as well. I
guess the key is in reversing the list ;)
On 10/12/07, graemebe <> wrote:
> I've just started learning erlang. I wanted to create a set of processes
> keep their pids in a list and send a message to each with a list
> comprehension. In general, is the spawnem function I hacked out below an
> efficient way to build a list?
> -export([start/0, recv/0, spawnem/1]).
> % Receive a message and print it, repeat.
> recv() ->
> Msg -> io:fwrite("~p\n", [Msg]),
> % Spawn some processes and send a message to each.
> start() ->
> [X ! "hello world!" || X <- P].
> % Function to Spawn N processes
> spawnem(N) when is_integer(N), N > 0 -> spawnem_acc(N, [spawn(fun
> spawnem_acc(N, L) when is_integer(N), N > 1 -> spawnem_acc(N-1, [spawn(fun
> ring:recv/0)] ++ L);
> spawnem_acc(1, L) -> L.
> Any comments/optimisations appreciated!
> View this message in context:
> Sent from the Erlang Questions mailing list archive at Nabble.com.
> erlang-questions mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions