monads

Simplest non-trivial monad transformer example for “dummies”, IO+Maybe

南楼画角 提交于 2019-11-26 22:31:19
问题 Could someone give a super simple (few lines) monad transformer example, which is non-trivial (i.e. not using the Identity monad - that I understand). For example, how would someone create a monad that does IO and can handle failure (Maybe)? What would be the simplest example that would demonstrate this? I have skimmed through a few monad transformer tutorials and they all seem to use State Monad or Parsers or something complicated (for a newbee). I would like to see something simpler than

How to convert A[B[C]] to B[A[C]] if A and B are monads?

好久不见. 提交于 2019-11-26 20:57:05
I'm looking for a more general solution which exploits monads (and monoids possibly) to achieve the same as if( xs.contains(None) ) None else Some(xs.flatten) does for xs of type Seq[Option[A]] . How can I do that with Scalaz? I feel like I'm missing something evident. Having two monads is both not enough (for M ) and more than enough (for N )—which adds up to not enough, of course—but if M has a Traverse instance and N has an Applicative instance, you can use sequence . For example: import scalaz._, Scalaz._ def foo[A](xs: List[Option[A]]): Option[List[A]] = xs.sequence This has the semantics

How to get normal value from IO action in Haskell

时光总嘲笑我的痴心妄想 提交于 2019-11-26 20:55:22
I have the following function: get :: Chars -> IO Chars get cs = do char <- getChar let (dats, idx) = (curData cs, curIndex cs) let (x,y:xs) = splitAt idx dats let replacement = x ++ (ord char) : xs return $ Chars replacement idx and I'd like to get a Chars value out of it, not an IO action. I have no idea how to do this, or if it is even possible. Chars is basically just a container with an [Int] called curData and an Int called curIndex. The specifics aren't that important, I just want to know if there's a way for this function to return a Chars instead of an IO Chars . If not, how do I pass

How to extract value from monadic action

萝らか妹 提交于 2019-11-26 20:41:36
Is there a built-in function with signature :: (Monad m) => m a -> a ? Hoogle tells that there is no such function. Can you explain why? A monad only supplies two functions: return :: Monad m => a -> m a (>>=) :: Monad m => m a -> (a -> m b) -> m b Both of these return something of type m a , so there is no way to combine these in any way to get a function of type Monad m => m a -> a . To do that, you'll need more than these two functions, so you need to know more about m than that it's a monad. For example, the Identity monad has runIdentity :: Identity a -> a , and several monads have

How do pipes and monads work together in JavaScript?

自闭症网瘾萝莉.ら 提交于 2019-11-26 20:25:20
I have looked at similar questions and answers and have not found an answer that directly addresses my question. I am struggling to understand how to use Maybe or Either or Monads in conjunction with piping functions. I want to pipe functions together, but I want the pipe to stop and return an error if one occurs at any step. I am trying to implement Functional Programming concepts in a node.js app, and this is really my first serious exploration of either, so no answer will be so simple as to insult my intelligence on the subject. I have written a pipe function like this: const _pipe = (f, g)

What’s an example of a Monad which is an Alternative but not a MonadPlus?

ε祈祈猫儿з 提交于 2019-11-26 19:36:41
问题 In his answer to the question “Distinction between typeclasses MonadPlus, Alternative, and Monoid?”, Edward Kmett says that Moreover, even if Applicative was a superclass of Monad , you’d wind up needing the MonadPlus class anyways, because obeying empty <*> m = empty isn’t strictly enough to prove that empty >>= f = empty So claiming that something is a MonadPlus is stronger than claiming it is Alternative . It’s clear that any applicative functor which is not a monad is automatically an

What exactly does “effectful” mean

爷,独闯天下 提交于 2019-11-26 19:25:47
问题 Time and again I read the term effectful , but I am still unable to give a clear definition of what it means. I assume the correct context is effectful computations , but I've also seen the term effectful values ) I used to think that effectful means having side effects . But in Haskell there are no side-effects (except to some extent IO). Still there are effectful computations all over the place. Then I read that monads are used to create effectful computations. I can somewhat understand

Monads as adjunctions

南笙酒味 提交于 2019-11-26 18:49:25
问题 I've been reading about monads in category theory. One definition of monads uses a pair of adjoint functors. A monad is defined by a round-trip using those functors. Apparently adjunctions are very important in category theory, but I haven't seen any explanation of Haskell monads in terms of adjoint functors. Has anyone given it a thought? 回答1: Edit : Just for fun, I'm going to do this right. Original answer preserved below The current adjunction code for category-extras now is in the

Haskell - depth for each node in binary tree using Reader monad

杀马特。学长 韩版系。学妹 提交于 2019-11-26 17:52:18
问题 I wrote the following code. It is working and using the Reader monad. Could you give me some hints about code style in Haskell ? Mainly, I mean monads -- I am newbie. import Control.Monad.Reader data Tree a = Node a (Tree a) (Tree a) | Empty renumberM :: Tree a -> Reader Int (Tree Int) renumberM (Node _ l r) = ask >>= (\x -> return (Node x (runReader (local (+1) (renumberM l)) x) (runReader (local (+1) (renumberM r)) x))) renumberM Empty = return Empty renumber'' :: Tree a -> Tree Int

What is a monad?

假装没事ソ 提交于 2019-11-26 17:29:05
问题 Having briefly looked at Haskell recently, what would be a brief, succinct, practical explanation as to what a monad essentially is? I have found most explanations I've come across to be fairly inaccessible and lacking in practical detail. 回答1: First: The term monad is a bit vacuous if you are not a mathematician. An alternative term is computation builder which is a bit more descriptive of what they are actually useful for. You ask for practical examples: Example 1: List comprehension : [x*2