You can use MaybeT and the MonadPlus instance to use msum: f :: MaybeT IO Int f = msum [readInt, readInt, readInt] ...

Hamlet is just providing an alternate syntax for normal Haskell code, so like normal Haskell, you have to keep your impure actions separate. In other words, you need to run the IO action outside of the template.

The getStdRandom function basically looks up a StdGen value in a global variable, runs some function on it, puts the new seed back into the global variable, and returns the result to the caller. If the function in question returns with an error, that error gets put into the global...

Most of the standard library list functions have monadic versions that end with M: map :: (a -> b) -> [a] -> [b] mapM :: (Monad m) => (a -> m b) -> [a] -> m [b] replicate :: Int -> a -> [a] replicateM :: (Monad m) => Int...

sockets,haskell,network-programming,io-monad

The idiomatic way to repeat the same action over and over again forever is forever serverLoop :: Socket -> IO () serverLoop sock = forever $ do (conn, _) <- accept sock forkIO $ handleConn conn ...

Any time you end up with [IO Foobar], what you probably want is actually IO [Foobar]. The sequence function transforms one into the other. Or you can use mapM instead of map when creating the list in the first place. In your example, it's a little more complicated, since we...

haskell,types,monads,algebraic-data-types,io-monad

You want something like newtype CWIO a = CWIO { unCWIO :: CWorld -> (a, CWorld) } which will allow you to define the appropriate instances and to use CWIO and unCWIO to move back and forth between wrapped CWIO computations and the unwrapped underlying functions. newtype is a variant...

What about exploiting the IO monad: func [_] = return () func (_:xs) = print xs >> func xs When one calls this it results in: *Main> func [1, 8, 90, 100, 82] [8,90,100,82] [90,100,82] [100,82] [82] As you can read here, the return can be seen as the "no...

A possible solution (importing System.IO System.Directory System.FilePath Control.Applicative): concat <$> mapM (\pd -> map (pd </>) <$> getDirectoryContents pd) pathDirs -- ^ this is to work under IO (..) -- ^ this is to affect what's under IO [..] -- ^ this returns IO [[FilePath]] There might be some way...

You can combine different monads using monad transformers - in this case StateT. You can use your existing code by changing the type signatures to use StateT: bigger, smaller :: Monad m => StateT Bound m () then you can write a function to run the game given a state...

The short answer is Futures. In order to be non-blocking your IO operations should return Futures that you map to other Futures. In Spray you can complete the request with a Future.

Since you're using IO inside choseRandom you need to change the type signature: choseRandom :: [a] -> IO a secondly, you don't need to use >>= to get the length of the list. >>= has type Monad m => m a -> (a -> m b) -> m b The...