[erlang-questions] Question on building a variable with multiple math operations

Illo de' Illis <>
Fri Jul 10 17:29:18 CEST 2009


On Jul 10, 2009, at 5:17 PM, Greg Perry wrote:

> [...] The list filled with tuples that returns is variable in  
> length, depending on the properties of any given N.  I would like to  
> be able to build a variable that takes each tuple from a list and  
> multiplies them together, then adds them to the other products.  For  
> example, if N yields properties of [{1,5},{2,7},{4,8}], I need to be  
> able to build a variable which is: (1 * 5) + (2 * 7) + (4 * 8); some  
> N will return a list as small as three tuples, other N will return a  
> list that may have as many as nine tuples that need to be multiplied  
> in that fashion.
>
> I have been experimenting with lists:fold1 but it seems that is  
> expecting a simple list without the use of tuples.
>
> Any ideas on this one?

Isn't it as simple as doing:

lists:foldl(fun erlang:'+'/2, 0, [X*Y || {X,Y} <- N1]).

?

Ciao,
Illo.


More information about the erlang-questions mailing list