monads

What are the alternative of monads to use IO in pure functional programming?

monads are described as the haskell solution to deal with IO. I was wondering if there were other ways to deal with IO in pure functional language. ...

Using MonadError with Parsec

I'm trying to use MonadError together with Parsec. I've come up with the following code snippet: f5 = do char 'a' throwError "SomeError" f6 = f5 `catchError` (\e -> unexpected $ "Got the error: " ++ e) ret = runErrorT (runParserT f6 () "stdin" "a") However, ret is Left "SomeError", it seems the catchError doesn't have any ef...

How can I use parMap with a monadic function?

I have a monadic function getRate: getRate :: String -> IO Double I'd like to map this function over a list of String's. Normally, I would just do: mapM getRate ["foo", "bar"] but since each call to getRate makes network calls, I'd like to parallelize the map so that each rate is fetched in a separate thread (or at least spread ou...

Composing monad actions with folds

Let's take a function of type (Monad m) => a -> m a. For example: ghci> let f x = Just (x+1) I'd like to be able to apply it any number of times. The first thing I tried was ghci> let times n f = foldr (>=>) return $ replicate n f The problem is that it won't work for large n: ghci> 3 `times` f $ 1 Just 4 ghci> 1000000 `times` f $...

How do VBOs/FBOs/DisplayLists work in Haskell's OpenGl bindings?

Haskell is about computation by calculation of values. DisplayLists / FBOs / VBOs are very very stateful by nature. I.e. "give me a display list / buffer object". How do these bindings work in Haskell? [I do understand monads; so a technical explaination, as opposed to a fluffy one, is preferred]. Thanks! ...

Resources for learning Monads, Functors, Monoids, Arrows etc

Can you people please suggest some good books / weblinks from where I can get to learn about above mentioned concepts? (Please note that I am a Java programmer and have NO prior experience with functional programming. I have been studying Scala since last one month and would appreciate the resources that try to teach the above mentione...

How do i convert String to Integer/Float in Haskell

data GroceryItem = CartItem ItemName Price Quantity | StockItem ItemName Price Quantity makeGroceryItem :: String -> Float -> Int -> GroceryItem makeGroceryItem name price quantity = CartItem name price quantity I want to create a GroceryItem when using a String or [String] createGroceryItem :: [String] -> GroceryItem createGrocery...

Why are side-effects modeled as monads in Haskell?

Could anyone give some pointers on why the unpure computations in Haskell are modeled as monads? I mean monad is just an interface with 4 operations, so what was the reasoning to modeling side-effects in it? ...

The reason for MonadState get and put?

I'm reading the Monads chapter in Real World Haskell (chapter 14). A function is defined as follows: type RandomState a = State StdGen a getRandom :: Random a => RandomState a getRandom = get >>= \gen -> let (val, gen')= random gen in put gen' >> return val I don't really understand the purpose of the get and put funct...

Common practice for higher-order-polymorphism in scala

Hi, I'm trying to grasp higher-order-polymophism in scala by implementing a very basic interface that describes a monad but I come across a problem that I don't really understand. I implemented the same with C++ and the code looks like this: #include <iostream> template <typename T> class Value { private: T value; public: Value(c...

Why this Either-monad code does not type check?

instance Monad (Either a) where return = Left fail = Right Left x >>= f = f x Right x >>= _ = Right x this code frag in 'baby.hs' caused the horrible compilation error: Prelude> :l baby [1 of 1] Compiling Main ( baby.hs, interpreted ) baby.hs:2:18: Couldn't match expected type `a1' against inferred typ...

State Monad, why not a tuple?

I've just wrapped my head around monads (at least I'd like to think I have) and more specifically the state monad, which some people that are way smarter then me figured out, so I'm probably way of with this question. Anyway, the state monad is usually implemented with a M<'a> as something like this (F#): type State<'a, 'state> = State...

Type error while trying to implement the (>>=) function in order to create a custom monad transformer.

Hello, I'm trying to create a monad transformer for a future project, but unfortunately, my implementation of the Monad typeclasse's (>>=) function doesn't work. First of all, here is the underlying monad's implementation : newtype Runtime a = R { unR :: State EInfo a } deriving (Monad) Here, the implementation of the Monad type...

Are monads Writer m and Either e categorically dual?

I noticed there is a dual relation between Writer m and Either e monads. If m is a monoid, then unit :: () -> m join :: (m,m) -> m can be used to form a monad: return is composition: a -> ((),a) -> (m,a) join is composition: (m,(m,a)) -> ((m,m),a) -> (m,a) The dual of () is Void (empty type), the dual of product is coproduct. Every...

C# ambiguity in Func + extension methods + lambdas

I've been trying to make my way through this article: http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx ... And something on page 1 made me uncomfortable. In particular, I was trying to wrap my head around the Compose<>() function, and I wrote an example for myself. Consider the following two Func's: Func<d...

Monad in plain English? (For the OOP programmer with no FP background)

In terms that an OOP programmer would understand (without any functional programming background), what is a monad? What problem does it solve and what are the most common places it's used? EDIT: To clarify the kind of understanding I was looking for, let's say you were converting an FP application that had monads into an OOP applicati...

Can I transform this asynchronous java network API into a monadic representation (or something else idiomatic)?

I've been given a java api for connecting to and communicating over a proprietary bus using a callback based style. I'm currently implementing a proof-of-concept application in scala, and I'm trying to work out how I might produce a slightly more idiomatic scala interface. A typical (simplified) application might look something like thi...

Monad equivalent in Ruby

What would an equivalent construct of a monad be in Ruby? ...

Haskell and random numbers

Hi, I've been messing with Haskell few days and stumbled into a problem. I need a method that returns a random list of integers ( Rand [[Int]] ). So, I defined a type: type Rand a = StdGen -> (a, StdGen). I was able to produce Rand IO Integer and Rand [IO Integer] ( (returnR lst) :: StdGen -> ([IO Integer], StdGen) ) somehow. Any tip...

Has anyone ever encountered a Monad Transformer in the wild?

In my area of business - back office IT for a financial institution - it is very common for a software component to carry a global configuration around, to log it's progress, to have some kind of error handling / computation short circuit... Things that can be modelled nicely by Reader-, Writer-, Maybe-monads and the like in Haskell and ...