您可以提升组合函数以将 Foo 标记穿过,这样您仍然可以使用 scanl1 ,而不是编写令人不满意的高阶函数,这就是您的意思。
keeptags :: (Bar -> Bar -> Bar) -> (Foo,Bar) -> (Foo,Bar) -> (Foo,Bar)
keeptags g (_,b) (a',b') = (a',g b b')
现在你可以使用scanl1;带上你原来的qux :: Bar -> Bar -> Bar 并制作
scanQux :: [(Foo,Bar)] -> [(Foo,Bar)]
scanQux = scanl1 (keeptags qux)
keeptags 很简单,scanQux 非常清晰。
例如,如果
type Foo = Char
type Bar = Int
qux = (+)
然后你得到
*Main> scanl1 qux [1..9]
[1,3,6,10,15,21,28,36,45]
*Main> zip "HELLO MUM" [1..9]
[('H',1),('E',2),('L',3),('L',4),('O',5),(' ',6),('M',7),('U',8),('M',9)]
*Main> scanQux $ zip "HELLO MUM" [1..9]
[('H',1),('E',3),('L',6),('L',10),('O',15),(' ',21),('M',28),('U',36),('M',45)]
如你所愿。