Erlang article #1 on programming.reddit.com

Richard A. O'Keefe ok@REDACTED
Fri Aug 18 04:16:20 CEST 2006


I mentioned the
    (let N = 0 then N+1, S = 0 then S+X
     for X <- List, X > 0
     in  S/N
    )
construct that had been designed recently in this mailing list.

"Ryan Rawson" <ryanobjc@REDACTED> wrote:
	That's more of a list comprehension than a loop now is it?

It's as loopy a for loop as ever there was.
List comprehensions make lists.
This kind of loop *may* make a list, but needn't.

    [ E(X) || X <- L, G(X) ]

can be expressed in this form as

    (let R = [] then [E(X)|R]
     for X <- L, G(X)
     in  reverse(R)
    )

You can think of this as a multi-result fold + map + filter.
In other words, you can think of it as a for loop.

If you think a 'for' loop must have something to do with counting,
reflect that a compiler might recognise X <- lists:seq(1, N) and
do something special with it (if you promise faithfully not to change
the semantics of seq/2 next time you reload the lists module, of course...).



More information about the erlang-questions mailing list