[erlang-questions] Suggestion for the Mnesia manual

Kostis Sagonas kostis@REDACTED
Mon May 5 13:05:17 CEST 2008


Jouni Rynö wrote:
> So from analysis point of view, recursion still would be better? (sorry,
> deleted the original example, but this was directly from my own code,
> revealed by dialyzer ...)
> 
> Instead of this (dialyzer tells me, that it produces value of type
> ['ok'])
> [ok = gen_server:cast(Client, {parameter, Para}) || Client <-
> ClientList]
> 
> use the recursion
> serve_clients(Message, [Client|ClientList]) ->
> 	ok = gen_server:cast(Client, Message),
>           serve_clients(Message, ClientList);
> serve_clients(Message, []) ->
> 	ok.

The best way to write the above is using lists:foreach/2 as in:

   lists:foreach(fun (Client) ->
	           gen_server:cast(Client, {parameter, Para})
                 end, Clients)

which achieves what you want, in my opinion expresses the intention of 
the programmer clearer than a comprehension and makes dialyzer happy.

Kostis


PS. I am aware that in R12B, lists comprehensions are compiled in such a
     way that a list for the result is not constructed when the result is
     not used, but this is just a compiler optimization which happens at
     a later stage - not something formally defined in the language or in
     Core Erlang.



More information about the erlang-questions mailing list