list comprehension question

Charles Blair <>
Thu Jul 28 23:42:41 CEST 2005


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

thanks for pointing me to that. i'll remember to look at it in future.

> 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)]."

yes, but since list comprehensions seem to be inspired by set
comprehensions (where order has no meaning for a set), and since
Thompson, in Haskell: The Craft of Functional Programming, describes
the program for computing permutations by means of list comprehensions
in part as "If xs is not empty, a permutation is given by picking an
element x from xs and putting x at the front of the permutation of the
remainder ..."--he does not say "by picking the first element", and
this algorithm corresponds exactly to the one given in Programming
Examples 3.3, and since the documentation for lists:map says "The
evaluation order is implementation dependent" (and it is lists:map
which is implicated in 3.5), while that for lists:foreach says "This
function is used for its side effects and the evaluation order is
defined to be the same as the order of the elements in the
list,"--yes, it would have been odd if it didn't work that way, but i
felt i needed to make sure. thanks.




More information about the erlang-questions mailing list