Reputation: 79
I need to compute the partial sums of a list in haskell. Any ideas of how can I do that?
if I have
partialSum (1:3:5:[]) => (1:4:9:[])
partialSum [] => []
partialSum (from 1) => 1:3:6:10:...
Upvotes: 1
Views: 896
Reputation: 909
partialSum :: Num a => [a] -> [a]
partialSum = drop 1 . scanl (+) 0
λ> take 10 $ partialSum [1..]
[1,3,6,10,15,21,28,36,45,55]
EDIT: OP requested a version without scanl
:
partialSum :: Num a => [a] -> [a]
partialSum (x:xs) = x : map (+ x) (partialSum xs)
partialSum [] = []
Upvotes: 7