Haskell do clause with multiple monad types

一个人想着一个人 提交于 2019-12-04 03:20:37

A do block is for a specific type of monad, you can't just change the type in the middle.

You can either transform the action or you can nest it inside the do. Most times transformations will be ready for you. You can, for instance have a nested do that works with io and then convert it only at the point of interaction.

In your case, a liftIOLater function is offered to handle this for you by the ThreePennyUI package.

liftIOLater :: IO () -> UI ()

Schedule an IO action to be run later.

In order to perform the converse conversion, you can use runUI:

runUI :: Window -> UI a -> IO a

Execute an UI action in a particular browser window. Also runs all scheduled IO action.

This is more an extended comment - it doesn't address the main question, but your implementation of shufffle. There are 2 issues with it:

  1. Your implementation is inefficient - O(n^2).
  2. IO isn't the right type for it - shuffle has no general side effects, it just needs a source of randomness.

For (1) there are several solutions: One is to use Seq and its index, which is O(log n), which would make shuffle O(n log n). Or you could use ST arrays and one of the standard algorithms to get O(n).

For (2), all you need is threading a random generator, not full power of IO. There is already nice library MonadRandom that defines a monad (and a type-class) for randomized computations. And another package already provides the shuffle function. Since IO is an instance of MonadRandom, you can just use shuffle directly as a replacement for your function.

Under the cover, do is simply syntactic sugar for >>= (bind) and let:

do { x<-e; es } =   e >>= \x -> do { es }
do { e; es }    =   e >> do { es }
do { e }        =   e
do {let ds; es} =   let ds in do {es} 

And the type of bind:

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

So yeah it only "supports" one Monad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!