I like reading snippets of code about concepts that I don't understand. Are there any snippets that show off monads in all their glory? More importantly how can I apply monads to make my job easier.
I use jQuery heavily. That's one cool application of monads I know of.
...
Last week user Masse asked a question about recursively listing files in a directory in Haskell. My first thought was to try using monadic lists from the List package to avoid building the entire list in memory before the printing can start. I implemented this as follows:
module Main where
import Prelude hiding (filter)
import Control...
I've been experimenting with monads in Clojure and came up with the following code, where a monadic value/state pair is represented by a mutable Clojure deftype object.
Since the object is mutable, an advantage would seem to be that you can write monadic code without needing to construct new result objects all the time.
However, I'm pr...
I'm a newbie, and the monads get me totally confused. Given a list of filenames i'd like to know whether all the files exist.
Generally, i'd like to do:
import System.Directory
allFilesPresent files = foldr (&&) True (map doesFileExist files)
However i do not know what's the right way to do it, because there's IO Bool instead of Bool...
Consider the following code I wrote:
import Control.Monad
increasing :: Integer -> [Integer]
increasing n
| n == 1 = [1..9]
| otherwise = do let ps = increasing (n - 1)
let last = liftM2 mod ps [10]
let next = liftM2 (*) ps [10]
alternateEndings next last
whe...
I've just written this function which simply takes a pair whose second value is in some monad, and "pulls the monad out" to cover the whole pair.
unSndM :: Monad m => (a, m c) -> m (a, c)
unSndM (x, y) = do y' <- y
return (x, y')
Is there a nicer and/or shorter or point-free or even standard way to express this?
I'...
Hi all. My question is simple. Why wrong pattern matching does not throw exception in Maybe monad. For clarity :
data Task = HTTPTask {
getParams :: [B.ByteString],
postParams :: [B.ByteString],
rawPostData :: B.ByteString
} deriving (Show)
tryConstuctHTTPTask :: B.ByteString -> Maybe Task
tryConstuctHTTPTask str = do
case...
Hi,
I need to write a state monad that can also support error handling. I was thinking of using the Either monad for this purpose because it can also provide details about what caused the error. I found a definition for a state monad using the Maybe monad however I am unable to modify it to use Either, instead of Maybe. Here's the code:...
I've had the IO monad described to me as a State monad where the state is "the real world". The proponents of this approach to IO argue that this makes IO operations pure, as in referentially transparent. Why is that? From my perspective it appears that code inside the IO monad have plenty of observable side effects. Also, isn't it possi...