Unlike a Functor, a Monad can change shape?

前端 未结 8 2344
太阳男子
太阳男子 2021-02-18 21:29

I\'ve always enjoyed the following intuitive explanation of a monad\'s power relative to a functor: a monad can change shape; a functor cannot.

For example: length

8条回答
  •  孤街浪徒
    2021-02-18 21:51

    Just because the monad pattern includes some particular instances that allow shape changes doesn't mean every instance can have shape changes. For example, there is only one "shape" available in the Identity monad:

    newtype Identity a = Identity a
    instance Monad Identity where
        return = Identity
        Identity a >>= f = f a
    

    In fact, it's not clear to me that very many monads have meaningful "shape"s: for example, what does shape mean in the State, Reader, Writer, ST, STM, or IO monads?

提交回复
热议问题