Purpose of Reader Monad

I am having difficulty in understanding the Reader Monad. My understanding from all the places I read, is that it allows to share a variable(Read Mode) to different function. Below is the implementation of two functions computation and samething (sor...
more »

2017-03-23 17:03 (2) Answers

How infinite loop with IO work in haskell

In haskell, all the IO code that we write is just an action (many suggest to think it as a script being generated). It is the main method that finally executes them (execute the constructed script). So how does the following program work ? infi funct...
more »

2017-03-22 19:03 (4) Answers

Recursive liftIO

I've looked at the some instances of MonadTrans, for MaybeT the implementation looks like this: instance MonadTrans MaybeT where lift = MaybeT . liftM Just As I understand the instance for MonadIO is used to do a variable number of lifts from ...
more »

2017-03-21 12:03 (2) Answers

Which Algebraic Pattern fits this type of tree?

I've got a puzzle for you, I managed to write some code that would do these things using recursion-schemes, but it's incredibly messy and that usually means that I missed a useful abstraction somewhere. I'm designing a layout system for my text edi...
more »

2017-03-19 20:03 (2) Answers

Ambiguous occurrence `Monad'

I am currently working with Monad type classes, and here is my code: class Monad m where return :: a -> m a (>>==) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b x >> y = x >>...
more »

2017-03-19 13:03 (0) Answers

Why does this haskell code failed

First of all I don't exactly understand the error message, so I can only use a ambiguous question title This code is from Chapter 8 of Channel 9 lecture about programming in haskell I was trying to run this code by GHC and I wrote -- Functional p...
more »

2017-03-14 10:03 (1) Answers

Writer monad and unsequence

I am using the Writer monad to keep track of an error ("collision") flag on arbitrary values (such as Int). Once the flag is set it is "sticky" and attaches itself to all values produced as a result of any operation with the marked one. Sometimes th...
more »

2017-03-08 00:03 (1) Answers

State Monad mix IO and non-IO

I tried to use State Monad for simple variables and it work. But here it seems that I am trying mix IO and non-IO. The intention to have a array managed by the State Monad. I want to read and write to this array. Seems harder than I thought. ...
more »

2017-03-05 14:03 (1) Answers

Writer Monad Nested Twice

I am trying to nest writer monad twice using Monad Transformers. Here is a sketch: import Control.Monad.Identity import Control.Monad.Writer data Struct = S Bool instance Monoid Struct where mempty = S True mappend (S a) (S b) = S (a &...
more »

2017-03-04 00:03 (1) Answers

Why Do We Need Maybe Monad Over Either Monad

I was playing around Maybe and Either monad types (Chaining, applying conditional functions according to returned value, also returning error message which chained function has failed etc.). So it seemes to me like we can achieve same and more things...
more »

2017-03-02 12:03 (3) Answers

How to use the Select monad to solve n-queens?

I'm trying to understand how the Select monad works. Apparently, it is a cousin of Cont and it can be used for backtracking search. I have this list-based solution to the n-queens problem: -- All the ways of extracting an element from a list. oneOf...
more »

2017-02-21 22:02 (2) Answers

Does a Powerset-over-Reader monad exist?

The canonical 'Monad instance' for environment sharing plus nondeterminism is as follows (using pseudo-Haskell, since Haskell's Data.Set isn't, of course, monadic): eta :: a -> r -> {a} -- '{a}' means the type of a set of a's eta x = \r -> ...
more »

2017-02-16 01:02 (2) Answers

Monadic concatMap for Data.Vector?

I'm looking for something from the Data.Vector package which works like concatMap but works with monads. concatMap has type... (a -> Vector b) -> Vector a -> Vector b but I'm looking for something with type... (Monad m) => (a -> ...
more »

2017-02-14 20:02 (1) Answers

Precomposing monad transformers

Suppose I have two monad transformers T1 :: (* -> *) -> * -> * T2 :: (* -> *) -> * -> * with instances instance MonadTrans T1 instance MonadTrans T2 and some X :: (((* -> *) -> * -> *) -> ((* -> *) -> * ->...
more »

2017-02-13 16:02 (3) Answers

Uses and examples of monad transformers?

I'm going over Monad Transformers and I understand their main role is to provide a monadic container to hold monads, of different types, which provides a common interface from which to manipulate the 'nested' monads within a computation. I've attemp...
more »

2017-02-13 10:02 (3) Answers

Exception handling and monad transformers

I'm writing an application with a text-based user interface using brick. I'm trying to add exception handling to my event loop without success. I'd like that when an action fails in my app, an exception is thrown, caught, and a notification displayed...
more »

2017-02-12 14:02 (0) Answers

Operating infinite lists with strict monads

I have a function f :: [a] -> b that operates on infinite lists (e.g. take 5, takeWhile (< 100) . scanl (+) 0 and so on). I want to feed this function with values generated by strict monadic actions (e.g. randomIO). From this question, I've le...
more »

2017-02-11 06:02 (2) Answers

Should bind result in a Monad?

The following is an example from a Haskell tutorial instance Monad Maybe where return x = Just x Nothing >>= f = Nothing Just x >>= f = f x fail _ = Nothing However, I am confused by the Just x... line. Shoul...
more »

2017-02-11 00:02 (1) Answers

Nested list resulting from do statement

As part of the following code, I have two 'populations' (genetic algorithm). Each population is comprised of many partial solutions. Combining a partial solution from each population produces a whole solution, for which I am attempting to calculate a...
more »

2017-02-09 14:02 (2) Answers