monads

How can monads make my job easier? Show me some cool piece of code.

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. ...

Why is my code using monadic lists from the List package so slow?

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...

Is this a sensible monad for mutable state in Clojure?

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...

Find out whether all given files exists in haskell

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...

Haskell: How to simplify or eliminate liftM2?

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...

Does this simple Haskell function already have a well-known name?

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'...

Why pattern matching does not throw exception in Maybe monad.

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...

How can I write a state monad that does error handling as well?

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:...

In what sense is the IO Monad pure?

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...