The Haskell List: Type -> Type
constructor implements the Functor
typeclass with function fmap f = map f
. This functor that applies the morphism f
to each element of a list works, but there are other equally valid functors, for example fmap f = map f . rev
which first reverses the list before mapping over it. Is there a concept in category theory that expresses the fact that the first choice of functor is in some sense "more natural" for this type constructor?
$\begingroup$
$\endgroup$
0
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
1
Being a functor requires two properties:
fmap id = id
fmap (f . g) = fmap f . fmap g
The definition fmap
fails the first condition:
fmap id = map id . reverse = reverse != id
So that's not a functor at all!
And it even fails the second condition:
fmap (f . g) = map (f . g) . reverse
= map f . map g . reverse
= map f . fmap g
= reverse . map f . reverse . fmap g
= reverse . fmap f . fmap g
!= fmap f . fmap g
Which is quite clear if you think about it:
fmap (f . g)
reverses the list once and then maps its elements viaf . g
fmap f . fmap g
reverse the list, maps viag
, then reverses the list again and then maps viaf
!
-
$\begingroup$ Wow, brainfart! Thanks for clearing that up $\endgroup$ Oct 4, 2017 at 23:25