実際には、(foldl.foldr) f z xs === foldr f z (concat $ reverse xs)
.
連想操作であってもf
、パフォーマンスに影響を与える可能性があるため、アプリケーションの正しい順序が重要です。
私たちはから始めます
(foldl.foldr) f z xs
foldl (foldr f) z xs
と書いてg = foldr f
、[x1,x2,...,xn_1,xn] = xs
ちょっとの間、これは
(...((z `g` x1) `g` x2) ... `g` xn)
(`g` xn) ((`g` xn_1) ... ((`g` x1) z) ... )
foldr f z $ concat [xn,xn_1, ..., x1]
foldr f z $ concat $ reverse xs
したがって、あなたの場合、正しい削減シーケンスは
(foldl.foldr) 1 [[1,2,3],[4,5,6]]
4+(5+(6+( 1+(2+(3+ 1)))))
22
つまり、
Prelude> (foldl.foldr) (:) [] [[1..3],[4..6],[7..8]]
[7,8,4,5,6,1,2,3]
同様に、(foldl.foldl) f z xs == foldl f z $ concat xs
. でsnoc a b = a++[b]
、
Prelude> (foldl.foldl) snoc [] [[1..3],[4..6],[7..8]]
[1,2,3,4,5,6,7,8]
また、(foldl.foldl.foldl) f z xs == (foldl.foldl) (foldl f) z xs == foldl (foldl f) z $ concat xs == (foldl.foldl) f z $ concat xs == foldl f z $ concat (concat xs)
など:
Prelude> (foldl.foldl.foldl) snoc [] [[[1..3],[4..6]],[[7..8]]]
[1,2,3,4,5,6,7,8]
Prelude> (foldl.foldr.foldl) snoc [] [[[1..3],[4..6]],[[7..8]]]
[7,8,1,2,3,4,5,6]
Prelude> (foldl.foldl.foldr) (:) [] [[[1..3],[4..6]],[[7..8]]]
[7,8,4,5,6,1,2,3]