4

我目前已经定义了两个函数:

avg :: (Fractional e) => [e] -> e
avg e = uncurry (/) $ foldl' (\(a,l) v -> (a+v,l+1)) (0.0,0.0) e

avgOf :: (Fractional a) => Getting (Endo (Endo (a, a))) s a -> s -> a
avgOf g s = uncurry (/) $ foldlOf' g accfun (0.0,0.0) s
  where accfun (a,l) v = (a+v, l+1)

我的印象是必须有一种简单的方法来摆脱整个avgOf实现,并用一个简单的方法来代替它,它只是“提升”avg来处理镜头。

4

1 回答 1

3

你可以用partsOf整块布来建造它。

avgOf l xs = xs^..partsOf l.to avg
于 2013-06-17T20:30:11.820 回答