monads

How to use (->) instances of Monad and confusion about (->)

别说谁变了你拦得住时间么 提交于 2019-11-26 04:45:49
问题 At different questions I\'ve found hints in comments concerning using the (->) instance of Monads e.g. for realizing point-free style. As for me, this is a little too abstract. Ok, I\'ve seen Arrow instances on (->) and it seems to me, that (->) can be used in instance notations but not in type declarations (that would alone be stuff for another question). Has anyone examples using (->) as instance of Monad? Or a good link? Sorry if this question may already have been discussed here, but

Concrete example showing that monads are not closed under composition (with proof)?

*爱你&永不变心* 提交于 2019-11-26 04:36:54
问题 It is well-known that applicative functors are closed under composition but monads are not. However, I have been having trouble finding a concrete counterexample showing that monads do not always compose. This answer gives [String -> a] as an example of a non-monad. After playing around with it for a bit, I believe it intuitively, but that answer just says \"join cannot be implemented\" without really giving any justification. I would like something more formal. Of course there are lots of

IO happens out of order when using getLine and putStr

╄→гoц情女王★ 提交于 2019-11-26 04:25:33
问题 I\'m a Haskell beginner, I\'m just beginning to wrap my head around Monads, but I don\'t really get it yet. I\'m writing a game that consists of asking the user for input, and responding. Here is a simplified version of my function: getPoint :: IO Point getPoint = do putStr \"Enter x: \" xStr <- getLine putStr \"Enter y: \" yStr <- getLine return $ Point (read xStr) (read yStr) completeUserTurn :: (Board, Player) -> IO (Board, Player) completeUserTurn (board, player) = do putStr $ \"Enter

Monads with Join() instead of Bind()

浪子不回头ぞ 提交于 2019-11-26 03:37:15
问题 Monads are usually explained in turns of return and bind . However, I gather you can also implement bind in terms of join (and fmap ?) In programming languages lacking first-class functions, bind is excruciatingly awkward to use. join , on the other hand, looks quite easy. I\'m not completely sure I understand how join works, however. Obviously, it has the [Haskell] type join :: Monad m => m (m x) -> m x For the list monad, this is trivially and obviously concat . But for a general monad,

Monad in plain English? (For the OOP programmer with no FP background)

安稳与你 提交于 2019-11-26 03:27:09
问题 In terms that an OOP programmer would understand (without any functional programming background), what is a monad? What problem does it solve and what are the most common places it\'s used? EDIT: To clarify the kind of understanding I was looking for, let\'s say you were converting an FP application that had monads into an OOP application. What would you do to port the responsibilities of the monads to the OOP app? 回答1: UPDATE: This question was the subject of an immensely long blog series,

Applicatives compose, monads don&#39;t

♀尐吖头ヾ 提交于 2019-11-26 03:26:14
问题 Applicatives compose, monads don\'t. What does the above statement mean? And when is one preferable to other? 回答1: If we compare the types (<*>) :: Applicative a => a (s -> t) -> a s -> a t (>>=) :: Monad m => m s -> (s -> m t) -> m t we get a clue to what separates the two concepts. That (s -> m t) in the type of (>>=) shows that a value in s can determine the behaviour of a computation in m t . Monads allow interference between the value and computation layers. The (<*>) operator allows no

Method parameters validation in Scala, with for comprehension and monads

。_饼干妹妹 提交于 2019-11-26 02:18:38
问题 I\'m trying to validate the parameters of a method for nullity but i don\'t find the solution... Can someone tell me how to do? I\'m trying something like this: def buildNormalCategory(user: User, parent: Category, name: String, description: String): Either[Error,Category] = { val errors: Option[String] = for { _ <- Option(user).toRight(\"User is mandatory for a normal category\").right _ <- Option(parent).toRight(\"Parent category is mandatory for a normal category\").right _ <- Option(name)

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

百般思念 提交于 2019-11-26 02:01:53
问题 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

Good examples of Not a Functor/Functor/Applicative/Monad?

筅森魡賤 提交于 2019-11-26 00:31:18
问题 While explaining to someone what a type class X is I struggle to find good examples of data structures which are exactly X. So, I request examples for: A type constructor which is not a Functor. A type constructor which is a Functor, but not Applicative. A type constructor which is an Applicative, but is not a Monad. A type constructor which is a Monad. I think there are plenty examples of Monad everywhere, but a good example of Monad with some relation to previous examples could complete the

What is a monad?

我与影子孤独终老i 提交于 2019-11-26 00:17:09
问题 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