optimization of list comprehensions

Mats Cronqvist <>
Mon Feb 27 13:35:32 CET 2006



Ulf Wiger (AL/EAB) wrote:
>  
> Samuel Rivas wrote:
> 
>>Ulf Wiger (AL/EAB) wrote:
>>
>>
>>>I hesitate to say that it's bad practice, even
>>>though one will build a potentially large list
>>>unnecessarily, since it's actually looks a lot 
>>>nicer than using lists:foreach().
>>
>>  Why? List comprehension is a tool to construct lists, 
>>lists:foreach has different semantics.
> 
> [...]
> 
> 42 characters vs. 73, or seen another way:
> 7 characters of overhead vs 38.
> 
> I don't fault programmers for choosing the 
> construct that's both easiest to write and 
> read, even if it means building a list unnecessarily.

   i too see lots of use of list comprehensions where lists:foreach/2 would be 
more appropriate. i suspect the reason is twofold; lots of people are scared of 
funs, and to most people (including me) a list comprehension is clearer.

   perhaps something like this could be introduced;
(whatever(L) || L <- List)

   the value of which would be 'ok' (just like lists:foreach).


   mats



More information about the erlang-questions mailing list