[erlang-questions] Erlang list comprehension optimization

Björn Gustavsson <>
Wed Mar 17 08:30:02 CET 2010


On Wed, Mar 17, 2010 at 5:07 AM, Robert Virding <> wrote:
>
> There is one strange thing further up in the text:
>
> "Lists comprehensions still have a reputation for being slow. They
> used to be implemented using funs, which used to be slow."
>
> List comprehensions have never been implemented with funs, at least
> not when they were first introduced. I know.

Have a look the function lc_tq/3 starting at line 430 in R6B:

http://github.com/mfoemmel/erlang-otp/blob/b48c3645b0e28fe61cb53771d2b84bfc30d48bad/lib/compiler/src/sys_pre_expand.erl

It seems clear to me from the code that list comprehensions are
rewritten to an expression that uses a fun. Also see the comment
before the function. It says that because Erlang doesn't have letrecs, the
lambda (fun) is explicitly passed as an extra argument.

Nowadays, list comprehensions are translated to letrecs
(which are available in Core Erlang), and the letrecs are in
turn translated to ordinary recursive functions.

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list