Correct Usage of >> in Haskell

Just now being introduced to monads in Haskell and hit something of a roadblock with >>. >>= makes sense to me, as I can get the following out of Prelude: Prelude> Just 1 >>= (\ x -> Just (x+1)) Just 2 My understanding is ...
more »

2017-10-04 12:10 (2) Answers

Making Haskell Monads

I'm trying to create a very simple monad in Haskell. The monad does nothing special but holding a counter as state. module EmptyMonad ( EmptyMonad ) where import Control.Monad data EmptyMonad a = EmptyMonad { myValue :: a , myState :: Int ...
more »

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

Monadic type checker in Haskell

I'm writing a parser and a type checker in Haskell starting from BNFC. The main function of the type checker is implemented as follows: typecheck :: Program -> Err () typecheck (PDefs ds) = do env <- foldM (\env (DFun typ id args _ _) -&...
more »

2017-09-27 13:09 (1) Answers

Perform Monadic Computation N times

I'm implementing a function to perform a monadic computation N times. I've written the following code. performN :: Monad m => Int -> m t -> m [t] performN 0 m = return [] performN n m = do x1<- m x2<- if n == 1 then return [] ...
more »

2017-09-18 14:09 (3) Answers

Use of List Monad vs fmap

Are there any practical uses of the list monad that wouldn't just roll to a fmap? When would you use bind over fmap with the list monad? Like, for example, you can do [1,2,3] >>= return . ( + 1) but that's the same as (+1) <$> [1,2,3] - ...
more »

2017-09-09 19:09 (3) Answers

Haskell - how to avoid messing pure with IO

I am implementing some algorithm on haskell. This algorithm requires generating some data. I have a function of an algorithm which takes generation function as a parameter. For example, algorithm is just multiplying input data by n: algo :: a ->...
more »

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

Type class instance redefining

I am trying to answer this question: "Given the algebraic data type data Maybe a = Nothing | Just a pick the correct instance declaration that shows that the type constructor Maybe is a Monad." (taken from here:"DelftX: FP101x Introduction to Fun...
more »

2017-08-31 20:08 (1) Answers

Understanding the Fix datatype in Haskell

In this article about the Free Monads in Haskell we are given a Toy datatype defined by: data Toy b next = Output b next | Bell next | Done Fix is defined as follows: data Fix f = Fix (f (Fix f)) Which allows to nest Toy expressions by ...
more »

2017-08-28 11:08 (1) Answers

Haskell List Monad — generalize by parameter

Let's imagine a population (of humans). Each human has things, that have length (left arm, right arm, left leg, right leg). Each human has things, that may have width (chest, waist). There are much more parameters that a human may have. All measurem...
more »

2017-08-23 04:08 (1) Answers

Add a monad to the middle of a transformer stack

I'm trying to "semilift" an (ExceptT Error IO Foo) to an (ExceptT Error (StateT Bar IO) Baz). I've tried lift, fmap lift, and fmap return, and none work; is there a standard idiom here? > import Control.Monad.Except > import Control.Monad.Sta...
more »

2017-08-20 23:08 (1) Answers

Impose constraint on MonadIO return type

I'm trying to make an instance of MonadIO for a type that represents computations in the cloud. So liftIO would represent an IO action that is executed locally. transient-universe, the library that provides this, provides a function localIO that doe...
more »

2017-08-17 12:08 (0) Answers

Monadic "double-bind"

Is it possible to resolve y as a function of f and x, if the monads N and M are one-way? x :: M (N X) f :: X -> M (N Y) -- pattern matching not allowed, one-way monads implied y :: M (N Y) y = _ Full definitions: data X = X data Y = Y -- pa...
more »

2017-08-15 20:08 (0) Answers

Why does "return Nothing" return Nothing?

"return a" is supposed to wrap a in the context of some Monad: *Main> :i return class Applicative m => Monad (m :: * -> *) where ... return :: a -> m a ... -- Defined in ‘GHC.Base’ If I ask GHCI what the type of "retu...
more »

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

Chaining State Monad

I have a function step :: Int -> State Int Int step n = get >>= \x -> put (x `div` n) >> return (x `mod` n) λ> runState (step 25) 41 (16,1) How do I run a sequence of steps, with different values of n and each step using ...
more »

2017-08-01 12:08 (1) Answers