[erlang-questions] List comprehensions challenge ;-)

Tony Rogvall tony@REDACTED
Wed Jan 12 13:02:00 CET 2011


On 12 jan 2011, at 10.08, Arnaud GARCIA wrote:

> Hello,
> 
> Just playing with list comprehensions...
> 
> I was asking if it possible to do it without lists:sum, by using an
> anonymous function and add it directly in the list comprehensions ?
> 
> 26> [A+B || A <-[1,2,3],B <- [7,8,9,10,11]].
> [8,9,10,11,12,9,10,11,12,13,10,11,12,13,14]
> 30> lists:sum([A+B || A <-[1,2,3],B <- [7,8,9,10,11]]).
> 165
> 
> Any idea ?
> 
You need to fold it.
Out of topic, this problem have a solution that has better complexity.

> length(As)*lists:sum(Bs)+length(Bs)*lists:sum(As)
165

/Tony



> Thanks
> 
> Arnaud

"Have run Make so many times I dunno what's installed anymore"



More information about the erlang-questions mailing list