【发布时间】:2013-07-30 17:25:14
【问题描述】:
我一直在搞乱一些 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
现在,当我们列出列表时,我们会得到[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...
我不知道这是否是模式。
【问题讨论】:
标签: list function haskell higher-order-functions fold