Accumulation

The accumulate function itertools.accumulate gives a running total.

Addition

For example, let’s try accumulating the numbers (1, 2, 3, 4, …):

Each subsequent element of the result is produced by adding the next value from the input to the previous value of the result. So the results are:

And so on.

A function that produces this sort of running total in Haskell is called a scan. There are a bunch of scan functions; the one that corresponds most closely to itertools.accumulate is called scanl1. scanl1 in Data.List and Prelude

The first argument to scanl1 specifies what function we want to use for the aggregation. Here we’ve used (+) to get the same result as the Python function.

The func parameter

The Python function can also be used with an aggregation function; this is what the optional func parameter is for. Here we accumulate from the same sequence, this time using multiplication instead of addition:

To do the same in Haskell, we use scanl1 again, this using (*) instead of (+) as the first argument.

Join Type Classes for courses and projects to get you started and make you an expert in FP with Haskell.