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

Haskell StateT and ExceptT chain

I am not very good haskell programer. I am tasked to write a compiler at my university and I have choosen haskell, as it is good tool for that purpose. I am using monads StateT and ExcepT, so I have types: type Runner r s = StateT s (ExceptT LatteEr...
more »

2017-02-08 13:02 (1) Answers

Generic nested monad manipulation?

I've been given the following class interface: class Misty m where banana :: (a -> m b) -> m a -> m b unicorn :: a -> m a And now I'm required to modify this to enable: jellybean :: (Misty m) => m (m a) -> m a I believe t...
more »

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

How to chain a -> IO (m b) functions

There are functions with signatures like: a -> IO (m b) b -> IO (m c) c -> IO (m d) How do I chain them in a -> IO (m d) ? Practical application: say there a set of REST endpoints. Each of the return a value and next one is requir...
more »

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

Understanding forall in Monad '>>=' function?

Following this answer, I've implemented a generic lift function in my program: liftTupe :: (x -> c x) -> (a, b) -> (c a, c b) --This will error liftTuple :: (forall x. x -> c x) -> (a, b) -> (c a, c b) I understand, that in this...
more »

2017-02-05 09:02 (4) Answers

Use `seq` and print in haskell

I know that this is a little bit tricky but i wonder why it doesn't work! module Main where sillyDebug :: Int -> Int -> Int sillyDebug x y = (print x) `seq` (x + y) main :: IO () main = do print (sillyDebug 1 2) while its ideal is ...
more »

2017-02-02 05:02 (1) Answers

Snapframework routes from 'csv'

i am not sure if i should use eval for this but i want to dynamically load the routes in a SnapFramework from a csv file, like this: csv file route, file redirect, redirects.csv red, redirects2.csv Now, i want to say to the framework which file s...
more »

2017-01-26 15:01 (0) Answers

Using ReaderT and runReaderT with SQLite?

I've the below code, taken from here: type Blog a = ReaderT SQLiteHandle IO a data BlogDBException = BlogDBException String deriving (Show, Typeable) instance Exception BlogDBException run :: Blog a -> IO a run m = do db <- openConnectio...
more »

2017-01-24 11:01 (1) Answers

Retain Laziness With mapM

consider the following simple IO function: req :: IO [Integer] req = do print "x" return [1,2,3] In reality this might be a http request, which returns a list after parsing it's result. I'm trying to concatenate the results of several calls o...
more »

2017-01-23 19:01 (5) Answers

How to interact with pure algorithm in IO code

To illustrate the point with a trivial example, say I have implemented filter: filter :: (a -> Bool) -> [a] -> [a] And I have a predicate p that interacts with the real world: p :: a -> IO Bool How do it make it work with filter wit...
more »

2017-01-23 18:01 (2) Answers

Implementing an MFunctor instance for RVarT

Is it possible to implement an MFunctor instance for RVarT? So far I've come up with the following: {-# LANGUAGE RankNTypes #-} import Data.RVar -- from rvar import Control.Monad.Trans.Class (lift) -- from transformers hoist...
more »

2017-01-23 00:01 (1) Answers