[erlang-questions] Code generation (was NOOB)

Robert Virding <>
Tue Sep 5 23:32:10 CEST 2006

Jay Nelson wrote:
> Bjorn clarified some optimization approaches:
>  >  We have considered improving the optimization, so that no list would
>>  be built in case 2.
> Where case 2 was the line commented <2> below:
>> foo(X) ->
>>    A = [do_something(E) || E <- X],    % <1>
>>    [do_other(E) || E <- X],            % <2>
>>    ok.

You must clarify what you mean is the exact difference between <1> and 
<2>? Is it the explicit assignment to A? Or that the return value is 
never used?

If it is the assignment then that is a very "sub-optimal" (to be nice) 
way of ascertaining whether the return value is used. The compiler has 
no difficult in finding out if the return value form the lc (the list) 
is actually used so checking for as assignment is useless.

It should not cause any problems if the actual list was not built, just 
as long as the code to build the list elements is executed for each 
element-not-to-be. The code can contain side-effects or cause an error.

Actually, the only difference between <1> and <2> is that <1> calls 
do_something on each element of X, while <2> calls do_other.


P.S. I think you should use code which builds a list when you want to 
build a list, not for side-effects! But then again I am a purist.

More information about the erlang-questions mailing list