Erlang list comprehension optimization

Vinayak Pawar <>
Wed Mar 17 04:17:12 CET 2010

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?

In other words, is it OK to use list comprehension in place of
lists:foreach/2 without any extra memory utilization?

Thanks in advance,

More information about the erlang-questions mailing list