[Haskell-cafe] cumulative sum

2006-10-02 Thread Tamas K Papp
Hi,

I have two lists, p and lambda (both are finite).  I would like to
calculate

1) the cumulative sum of lambda, ie if

lambda = [lambda1,lambda2,lambda3,...]

then

csum lambda = [lambda1,lambda1+lambda2,lambda1+lambda2+lambda3,...]

2) the cumulative sum of p*lambda (multiplication elementwise)

Once I know how to do the first, I know how to do the second I guess
(doing the multiplication using zipWith to get the p*lambda list, but
I would be interested in any other suggestions).  Currently I take and
sum, but then I am calculating the same sums many times.

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] cumulative sum

2006-10-02 Thread Michael Shulman

On 10/2/06, Tamas K Papp [EMAIL PROTECTED] wrote:

Hi,

I have two lists, p and lambda (both are finite).  I would like to
calculate

1) the cumulative sum of lambda, ie if

lambda = [lambda1,lambda2,lambda3,...]

then

csum lambda = [lambda1,lambda1+lambda2,lambda1+lambda2+lambda3,...]


Try (scanl1 (+) lambda)

Mike
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] cumulative sum

2006-10-02 Thread Chad Scherrer

Tamas,

try
scanl (+) 0
for the cumulative sum


From there the zipWith idea you mentioned seems like the way to go.


-Chad


Hi,

I have two lists, p and lambda (both are finite).  I would like to
calculate

1) the cumulative sum of lambda, ie if

lambda = [lambda1,lambda2,lambda3,...]

then

csum lambda = [lambda1,lambda1+lambda2,lambda1+lambda2+lambda3,...]

2) the cumulative sum of p*lambda (multiplication elementwise)

Once I know how to do the first, I know how to do the second I guess
(doing the multiplication using zipWith to get the p*lambda list, but
I would be interested in any other suggestions).  Currently I take and
sum, but then I am calculating the same sums many times.

Thanks,

Tamas

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] cumulative sum

2006-10-02 Thread Matthias Fischmann


 try
 scanl (+) 0
 for the cumulative sum

and it is probably worth pointing out once more that (as i have
learned only recently :-) Hoogle can help you even quicker than this
list with questions like these: scanl is the fifth answer if you ask
for a - [a] - [a].

also, the url is easy to remember: http://www.haskell.org/hoogle/


cheers,
m.


signature.asc
Description: Digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe