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

Return case of in a do block

So I'm new to Haskell and probably asking a really silly question. I try to get the following code to run: asdf :: Maybe a -> Maybe a asdf k = do return $ case k of Nothing -> Nothing Just x -> Just x ...
more »

2017-07-31 18:07 (1) Answers

Haskell: variable scope in list monad

I was reading an online Haskell book about list monad. In the book, the list monad was defined like this: instance Monad [] where return x = [x] xs >>= f = concat (map f xs) fail _ = [] And then there was an example of list...
more »

2017-07-28 18:07 (3) Answers

simple example of monad that takes integers

I am writing a Monad that composes functions, f and g, based on this explanation of Monads (https://www.youtube.com/watch?v=ZhuHCtR3xq8). the explanation stops at a crucial part: to compose a function (say f) a -> ma with another (say g) a -> m...
more »

2017-07-28 07:07 (2) Answers