扫描仪如何工作?哈斯克尔

Tha*_*tos 7 haskell function list fold higher-order-functions

我一直在搞乱一些Haskell函数,有些我已经理解,有些则没有.

例如,如果我们这样做:scanl (+) 0 [1..3]我的理解如下:

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
Run Code Online (Sandbox Code Playgroud)

现在,当我们制作清单时,我们得到了[0, 1, 3, 6].

但我似乎无法理解如何scanr (+) 0 [1..3]给我:[6,5,3,0] 也许scanr以下列方式工作?

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...
Run Code Online (Sandbox Code Playgroud)

我不知道这是不是模式.

Wil*_*ess 9

scanrfoldr什么scanlfoldl.foldr从右边开始:

foldr (+) 0 [1,2,3] =
  (1 + (2 + (3 + 0))) =
  (1 + (2 + 3)) =
  (1 + 5) =
  6
Run Code Online (Sandbox Code Playgroud)

并按scanr顺序显示中期结果:[6,5,3,0].