Unlike a Functor, a Monad can change shape?

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

    Does

    h :: (Monad m, Num a) => a -> m a
    h 0 = fail "Failed."
    h a = return a
    

    suit your needs? For example,

    > [0,1,2,3] >>= h
    [1,2,3]
    

提交回复
热议问题