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 ;)
<br><br><div><span class="gmail_quote">On 10/12/07, <b class="gmail_sendername">graemebe</b> <<a href="mailto:graemebe@hotmail.com">graemebe@hotmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>I've just started learning erlang. I wanted to create a set of processes and<br>keep their pids in a list and send a message to each with a list<br>comprehension. In general, is the spawnem function I hacked out below an
<br>efficient way to build a list?<br><br>-module(ring).<br>-export([start/0, recv/0, spawnem/1]).<br><br>% Receive a message and print it, repeat.<br>%<br>recv() -><br> receive<br> Msg -> io:fwrite("~p\n", [Msg]),
<br> recv()<br> end.<br><br>% Spawn some processes and send a message to each.<br>%<br>start() -><br> M=10,<br> P=ring:spawnem(M),<br> [X ! "hello world!" || X <- P].
<br><br>% Function to Spawn N processes<br>%<br>spawnem(N) when is_integer(N), N > 0 -> spawnem_acc(N, [spawn(fun<br>ring:recv/0)]).<br>spawnem_acc(N, L) when is_integer(N), N > 1 -> spawnem_acc(N-1, [spawn(fun
<br>ring:recv/0)] ++ L);<br>spawnem_acc(1, L) -> L.<br><br><br>Any comments/optimisations appreciated!<br><br>Cheers,<br>Graeme.<br><br><br><br><br><br>--<br>View this message in context: <a href="http://www.nabble.com/Learning-erlang---is-this-an-efficient-way-to-build-a-list--tf4540288.html#a12958075">
http://www.nabble.com/Learning-erlang---is-this-an-efficient-way-to-build-a-list--tf4540288.html#a12958075</a><br>Sent from the Erlang Questions mailing list archive at <a href="http://Nabble.com">Nabble.com</a>.<br><br>_______________________________________________
<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions">http://www.erlang.org/mailman/listinfo/erlang-questions
</a><br></blockquote></div><br>