
Hi, I was browsing through the source code for Data.Foldable and having trouble comprehending it (which was kind of the point of browsing the code, so I could learn something ;) ) I'm looking at foldl foldl :: (c -> d -> c) -> c -> t d -> c foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z But, I haven't got very far. I'm still trying to follow: Endo . flip f f is of type c->d->c, so this makes flip f of type d->c->c. I think the Endo constructor is of type (a->a)->Endo a I think a is binding to a function type here, but can not work out what. (From memory) ghci reports
:t Endo . flip (+) Num a => a -> Endo a
So, this looks like a partial application of the constructor? But, this still isn't helping me understand. Any thoughts or pointers that might help me comprehend what's happening? Thanks, Levi lstephen.wordpress.com

Just expand out the function composition:
Dual . Endo . flip f = (\x -> Dual (Endo (flip f x)))
which has the type d -> Dual (Endo c).
-- ryan
On 8/26/07, Levi Stephen
Hi,
I was browsing through the source code for Data.Foldable and having trouble comprehending it (which was kind of the point of browsing the code, so I could learn something ;) )
I'm looking at foldl
foldl :: (c -> d -> c) -> c -> t d -> c foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z
But, I haven't got very far. I'm still trying to follow:
Endo . flip f
f is of type c->d->c, so this makes flip f of type d->c->c.
I think the Endo constructor is of type (a->a)->Endo a
I think a is binding to a function type here, but can not work out what.
(From memory) ghci reports
:t Endo . flip (+) Num a => a -> Endo a
So, this looks like a partial application of the constructor?
But, this still isn't helping me understand.
Any thoughts or pointers that might help me comprehend what's happening?
Thanks, Levi lstephen.wordpress.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Ryan Ingram wrote:
Just expand out the function composition: Dual . Endo . flip f = (\x -> Dual (Endo (flip f x))) which has the type d -> Dual (Endo c).
-- ryan
Aha. I didn't get this straight away, but once I looked at the type signature for function composition, it became clear. Thanks, Levi
participants (2)
-
Levi Stephen
-
Ryan Ingram