[a]
is isomorphic to the quotient type of Maybe [a]
with Nothing
and Just []
considered equivalent. Alternatively it is isomorphic to Maybe (NonEmpty a)
, which simply eliminates the Just []
case.
In other words, [a]
can be factorized as a composition of the Maybe
and NonEmpty
functors.
A result of this is that you can lift any function on NonEmpty a
to a function on [a]
:
liftEmptyable :: (NonEmpty a -> r) -> [a] -> Maybe r
liftEmptyable _ [] = Nothing
liftEmptyable f (x:xs) = Just $ f (x:|xs)
Not sure that actually has much to do with your question though. As duplode answered, you don't really do anything but a simple functor mapping. We could at most elaborate that the monad laws ensure that the fmap
really behaves as if length
acted directly on the contained list:
Just [1,2] >>= return . length
≡ return [1,2] >>= return . length -- def. of `Monad Maybe`
≡ return (length [1,2]) -- left-identity monad law