[erlang-questions] Re: List comprehension questions

Robert Virding <>
Thu Mar 31 19:40:09 CEST 2011


List comprehensions work as a combined lists:map and lists:filter. So you map over a list filtering out elements you don't want in the output list. They have the added functionality that they can handle this for nested lists. 


The expanded code is in fact quite simple and is close to what you probably write yourself. 

So order is preserved and their is no call to lists:reverse when generating the output list. 

Robert 

----- "Olivier BOUDEVILLE" <> wrote: 
> 
> Hi, 
> 
> Reading http://www.erlang.org/doc/reference_manual/expressions.html#id77052 it is not obvious that the order induced by a single generator is necessarily preserved by the comprehension, as nevertheless suggested by all experiments like: 
> 
> 1> [ X || X <- [1,2,3] ]. 
> [1,2,3] 
> 
> I suppose this order preservation is true. Then, I imagine that, internally, list comprehensions are translated to code very similar to the one that would be produced if the developer had written an appropriate accumulator-based tail-recursive function? 
> 
> If so, does it imply that using a list comprehension on a semantically unordered set of elements will involve a useless reversal (like lists:reverse/1) of the resulting list? 
> 
> Thanks in advance for any answer, 
> Best regards, 
> 
> Olivier Boudeville. 
> --------------------------- 
> Olivier Boudeville 
> 
> EDF R&D : 1, avenue du Général de Gaulle, 92140 Clamart, France 
> Département SINETICS, groupe ASICS (I2A), bureau B-226 
> Office : +33 1 47 65 59 58 / Mobile : +33 6 16 83 37 22 / Fax : +33 1 47 65 27 13 
> 
> 
> Ce message et toutes les pièces jointes (ci-après le 'Message') sont établis à l'intention exclusive des destinataires et les informations qui y figurent sont strictement confidentielles. Toute utilisation de ce Message non conforme à sa destination, toute diffusion ou toute publication totale ou partielle, est interdite sauf autorisation expresse. 
> 
> Si vous n'êtes pas le destinataire de ce Message, il vous est interdit de le copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie. Si vous avez reçu ce Message par erreur, merci de le supprimer de votre système, ainsi que toutes ses copies, et de n'en garder aucune trace sur quelque support que ce soit. Nous vous remercions également d'en avertir immédiatement l'expéditeur par retour du message. 
> 
> Il est impossible de garantir que les communications par messagerie électronique arrivent en temps utile, sont sécurisées ou dénuées de toute erreur ou virus. 
> ____________________________________________________ 
> 
> This message and any attachments (the 'Message') are intended solely for the addressees. The information contained in this Message is confidential. Any use of information contained in this Message not in accord with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval. 
> 
> If you are not the addressee, you may not copy, forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your system and notify the sender immediately by return message. 
> 
> E-mail communication cannot be guaranteed to be timely secure, error or virus-free. 
> _______________________________________________ erlang-questions mailing list  http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110331/7ac25807/attachment.html>


More information about the erlang-questions mailing list