I have been messing with some Haskell functions, some I have understand and some don't.
For example if we do: scanl (+) 0 [1..3]
my understanding is the following:
1. the accumulator is 0 acc = 0 |
2. (+) applied to acc and first el acc = 0 + 1 = 1 |
3. (+) applied to latest acc and snd el acc = 1 + 2 = 3 |
4. (+) applied to latest acc and third acc = 3 + 3 = 6 V
Now when we make the list we get [0, 1, 3, 6]
.
But I can't seem to understand how does scanr (+) 0 [1..3]
gives me: [6,5,3,0]
Maybe scanr
works the following way?
1. the first element in the list is the sum of all other + acc
2. the second element is the sum from right to left (<-) of the last 2 elements
3. the third element is the sum of first 2...
I don't see if that's the pattern or not.