monads

Is monad bind (>>=) operator closer to function composition (chaining) or function application?

五迷三道 提交于 2019-11-26 09:45:31
问题 In many articles I have read that monad >>= operator is a way to represent function composition. But for me it is closer to some kind of advanced function application ($) :: (a -> b) -> a -> b (>>=) :: Monad m => m a -> (a -> m b) -> m b For composition we have (.) :: (b -> c) -> (a -> b) -> a -> c (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c Please clarify. 回答1: Clearly, >>= is not a way to represent function composition . Function composition is simply done with . . However, I

How to get normal value from IO action in Haskell

ε祈祈猫儿з 提交于 2019-11-26 09:03:36
问题 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

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

喜欢而已 提交于 2019-11-26 09:03:28
问题 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. 回答1: 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:

Operating on a return from a Maybe that contains “Just”

∥☆過路亽.° 提交于 2019-11-26 08:51:51
I have a function that has a return type of Maybe ([(Int,Int)],(Int,Int)) I would like to call this from another function and perform an operation on the data. However, the return value is contained within Just . The second method takes ([(Int,Int)],(Int,Int)) and therefore will not accept Just ([(Int,Int)],(Int,Int)) . Is there a way I can trim the Just before applying the second method? I don't fully understand the use of Just within Maybe - however, I have been told that the return type for the first Method must be Maybe . Thomas M. DuBuisson There are several solutions to your problem, all

What are free monads?

一个人想着一个人 提交于 2019-11-26 08:38:56
问题 I\'ve seen the term Free Monad pop up every now and then for some time, but everyone just seems to use/discuss them without giving an explanation of what they are. So: what are free monads? (I\'d say I\'m familiar with monads and the Haskell basics, but have only a very rough knowledge of category theory.) 回答1: Edward Kmett's answer is obviously great. But, it is a bit technical. Here is a perhaps more accessible explanation. Free monads are just a general way of turning functors into monads.

How to extract value from monadic action

眉间皱痕 提交于 2019-11-26 07:40:25
问题 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? 回答1: 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

What is indexed monad?

家住魔仙堡 提交于 2019-11-26 06:55:57
问题 What is indexed monad and the motivation for this monad? I have read that it helps to keep track of the side effects. But the type signature and documentation doesn\'t lead me to anywhere. What would be an example of how it can help to keep track of side effects (or any other valid example)? 回答1: As ever, the terminology people use is not entirely consistent. There's a variety of inspired-by-monads-but-strictly-speaking-isn't-quite notions. The term "indexed monad" is one of a number

How do pipes and monads work together in JavaScript?

不羁的心 提交于 2019-11-26 06:39:22
问题 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

A monad is just a monoid in the category of endofunctors, what&#39;s the problem?

大城市里の小女人 提交于 2019-11-26 04:56:55
问题 Who first said the following? A monad is just a monoid in the category of endofunctors, what\'s the problem? And on a less important note, is this true and if so could you give an explanation (hopefully one that can be understood by someone who doesn\'t have much Haskell experience)? 回答1: That particular phrasing is by James Iry, from his highly entertaining Brief, Incomplete and Mostly Wrong History of Programming Languages , in which he fictionally attributes it to Philip Wadler. The

A Haskell function of type: IO String-> String

别说谁变了你拦得住时间么 提交于 2019-11-26 04:46:56
问题 I wrote a bunch of code in Haskell to create an index of a text. The top function looks like this: index :: String -> [(String, [Integer])] index a = [...] Now I want to give this function a String read from a file: index readFile \"input.txt\" Which won\'t work because readFile is of type FilePath -> IO String. Couldn\'t match expected type \'String\' against inferred type \'IO String\' I see the error, but I can\'t find any function with type: IO String -> String I guess the key to success