What advantage does Monad give us over an Applicative?

后端 未结 8 602
心在旅途
心在旅途 2020-12-02 11:16

I\'ve read this article, but didn\'t understand last section.

The author says that Monad gives us context sensitivity, but it\'s possible to achieve the same result

8条回答
  •  孤街浪徒
    2020-12-02 11:58

    With monads, subsequent effects can depend on previous values. For example, you can have:

    main = do
        b <- readLn :: IO Bool
        if b
          then fireMissiles
          else return ()
    

    You can't do that with Applicatives - the result value of one effectfull computation can't determine what effect will follow.

    Somewhat related:

    • Why can applicative functors have side effects, but functors can't?
    • Good examples of Not a Functor/Functor/Applicative/Monad?

提交回复
热议问题