[erlang-questions] Is it better to use tuple to send the message or list or it just does not matter ?
Robert Virding
rvirding@REDACTED
Mon Dec 22 20:22:47 CET 2008
As you mentioned both alternatives work, you can send and receive anything,
and as Jacob was alluding to is the general rule that you use a tuple when
the size is known and fixed and use a list for dynamic collections. Also for
this type of usage a tuple is more efficient both in speed and memory size.
Robert
2008/12/22 lang qiu <qiulang@REDACTED>
> Hi all,
> When sending message by *Pid ! Message*, is it better to use *tuple *or *list
> *or it just does *not *matter ?
>
> For example (the area_server0 example in "programming erlang"),
>
> -module(area_server0).
> -export([loop/0]).
>
> loop() ->
> receive
> *{rectangle, Width, Ht}* -> % tuple here
> io:format("Area of rectangle is ~p~n" ,[Width * Ht]),
> loop();
> {circle, R} ->
> io:format("Area of circle is ~p~n" , [3.14159 * R * R]),
> loop();
> Other ->
> io:format("I don't know what the area of a ~p is ~n" ,[Other]),
> loop()
> end.
>
> Here we uses tuples to send/receive messages, but when I change them to
> list, the codes still work.
>
> loop() ->
> receive
> *[rectangle, Width, Ht]* ->
> ... % Still works as long as we agree to use list to send the
> message.
>
> So I was wondering does this mean either tuple or list is ok or there is
> still some difference between them ?
>
> Thanks!
>
> Qiulang
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081222/c03b4c61/attachment.htm>
More information about the erlang-questions
mailing list