list comprehension question

Ulf Wiger ulf@REDACTED
Thu Jul 28 22:56:38 CEST 2005


Den 2005-07-28 19:49:51 skrev Charles Blair <chas@REDACTED>:

> in something like this:
>
> [X || X <- lists:seq(1,25)]
>
> is the result guaranteed to reflect the order of the generator, or
> not?
>
> i'm working with a list of randomly generated results, and i want to
> print them in the order in which they were generated--is lists:foreach
> the way to go here? a list comprehension would be simpler to write and
> nicer to look at.

According to the erlang spec 4.7, page 94, the order between elements
must be preserved.

It's difficult to imagine the semantics of list comprehensions
without this guarantee. Besides, the programming examples section
in the erlang documentation implicitly states this as well:

(http://erlang.se/doc/doc-5.4.8/doc/programming_examples/list_comprehensions.html#3.5)

"As an example, list comprehensions can be used to simplify
some of the functions in lists.erl:

append(L)   ->  [X || L1 <- L, X <- L1].
map(Fun, L) -> [Fun(X) || X <- L].
filter(Pred, L) -> [X || X <- L, Pred(X)]."

/Uffe
-- 
Ulf Wiger



More information about the erlang-questions mailing list