Consider the following:
foldr (<>) seed [x1, x2, ... xn] == x1 <> (x2 <> (... <> (xn <> seed)))
Let’s just “cut” it into pieces:
(x1 <>) (x2 <>) ... (xn <>) seed
Now we have this bunch of functions, let them make them up:
(x1 <>).(x2 <>). ... .(xn <>).id $ seed
((.), id) it Endo monoid, therefore
foldr (<>) seed xs == (appEndo . foldr (mappend.Endo.(<>)) mempty $ xs) seed
For the left fold, we only need the monotone Dual .
leftFold (<>) seed xs = (appEndo . getDual . foldr (mappend . Dual . Endo . (<>)) mempty $ xs) seed
(<>) = (:) and seed = []
reverse' xs = (appEndo . getDual . foldr (mappend . Dual . Endo . (:)) mempty $ xs) []
Or simply:
reverse' xs = (appEndo . foldr (flip mappend . Endo . (:)) mempty $ xs) [] reverse' xs = (foldr (flip (.) . (:)) id $ xs) [] reverse' = flip (foldr (flip (.) . (:)) id) []
klapaucius
source share