Unlike a Functor, a Monad can change shape?

前端 未结 8 2352
太阳男子
太阳男子 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:59

    The key combinator for monads is (>>=). Knowing that it composes two monadic values and reading its type signature, the power of monads becomes more apparent:

    (>>=) :: Monad m => m a -> (a -> m b) -> m b
    

    The future action can depend entirely on the outcome of the first action, because it is a function of its result. This power comes at a price though: Functions in Haskell are entirely opaque, so there is no way for you to get any information about a composed action without actually running it. As a side note, this is where arrows come in.

提交回复
热议问题