[erlang-questions] Erlang list comprehension optimization

Bernard Duggan <>
Wed Mar 17 04:28:11 CET 2010


Vinayak Pawar wrote:
> Are these equivalent in terms of space utilization?
>
> 1) lists:foreach(fun(X) -> SomeOpFun(X) end, Xs).
>
> 2) [SomeOpFun(X) || X <- Xs].
>
> In case 1) output list isn't generated. In case 2) output list is generated,
> but it is not used(or not assigned to any variable). So in such cases, does
> Erlang VM optimize and doesn't create any temporary list?
>   
>From
http://www.erlang.org/doc/efficiency_guide/listHandling.html#id2261027 :

"In R12B [and presumably later], if the result of the list comprehension
will *obviously* not be used, a list will not be constructed."

Cheers,

Bernard


More information about the erlang-questions mailing list