How does forever monad work?

后端 未结 2 1088
天涯浪人
天涯浪人 2021-01-12 10:48

How does forever monad work?

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

If I write

main =          


        
2条回答
  •  长情又很酷
    2021-01-12 11:25

    The distinction to make is that putStrLn "SAD, I DON'T UNDERSTAND!" is an action, not just a value. It repeatedly executes that action. Whenever something of type IO a is evaluated, it executes its internal actions and then returns something of type a wrapped in the IO context. It doesn't have to take a parameter for the action to do something. For example, look at the getCurrentTime function from the time package. It just has type IO UTCTime, but if you call it several times you'll get different values back, even though it takes no parameters.

提交回复
热议问题