List comprehension or lists:foldl/3 ?

Thomas Arts <>
Fri Feb 13 08:29:48 CET 2004


Hi Shawn

I was just going to write basically the same as Christ wrote.
You are not interested in the result of the monitor calls. Hence,
why would you construct a list in memory, that is garbage 
directly after the call? 

>From your question I also deduced that you see foldl and
list-comprehension as similar concepts. However, a 
list-comprehension is more comparable to a map or
better a map with a filter application. Fold over a list
could well result in something completely different than
a list (e.g. a number). 

The lists:foreach/2 function is like a map when you
are only interested in the side-effects. I prefer that
one in this case. 

/Thomas

---
Dr Thomas Arts 
     Program Manager 
     Software Engineering and Management 
IT-university in Gothenburg 
Box 8718, 402 75 Gothenburg, Sweden 
http://www.ituniv.se/

Tel +46 31 772 6031 
Fax +46 31 772 4899 


----- Original Message ----- 
From: "Chris Pressey" <>
To: "Shawn Pearce" <>
Cc: <>
Sent: Friday, February 13, 2004 7:31 AM
Subject: Re: List comprehension or lists:foldl/3 ?


> On Fri, 13 Feb 2004 00:56:10 -0500
> Shawn Pearce <> wrote:
> 
> > Which is the more preferred form amongst you hacker types?
> > 
> > NodeList = [...],
> > [erlang:monitor(process, {myserv, N}) || N <- NodeList],
> > ok
> > 
> > or
> > 
> > NodeList = [...],
> > lists:foldl(
> > fun(N, _) -> erlang:monitor(process, {myserv, N}) end,
> > 0,
> > NodeList
> > ),
> > ok
> > 
> > I'm not concerned about the speed here, as its a startup routine
> > run once when the node boots.  Even for a very large cluster of
> > over 200+ nodes (highly unlikely in my application) this would take
> > no time either way.  This is a preferred syntax poll.
> > 
> > I find the LC easier to read by far.
> > 
> > -- 
> > Shawn.
> 
> I would probably use lists:foreach/2 instead just to drive home the fact
> that you're evaluating this for its side effects & don't care about the
> return value.  But that's just me.
> 
> -Chris
> 



More information about the erlang-questions mailing list