monads

Difference between Monads and Functions

风格不统一 提交于 2019-12-18 09:17:36
问题 Ok, about Monad , I am aware that there are enough questions having been asked. I am not trying to bother anyone to ask what is monad again. Actually, I read What is a monad?, it is very helpful. And I feel I am very close to really understand it. I create this question here is just to describe some of my thoughts on Monad and Function, and hope someone could correct me or confirm it correct. Some answers in that post let me feel monad is a little bit like function . Monad takes a type,

Why does haskell's bind function take a function from non-monadic to monadic

隐身守侯 提交于 2019-12-18 08:48:16
问题 I have some questions about the definition of the binding function (>>=) in Haskell. Because Haskell is a pure language, so we can use Monad to handle operations with side effects. I think this strategy is somewhat like putting all actions may cause side effects to another world, and we can control them from our "pure" haskell world though do or >>= . So when I look at definition of >>= function (>>=) :: Monad m => m a -> (a -> m b) -> m b it takes a (a -> m b) function, so result m a of the

Cannot find function similar to liftM2

江枫思渺然 提交于 2019-12-18 08:24:55
问题 myLiftM2 :: Monad m => (a -> a1 -> m b) -> m a -> m a1 -> m b myLiftM2 f x y = x >>= (\r1 -> y >>= (\r2 -> f r1 r2)) In liftM2 f return b, but myLiftM2 return m b 回答1: tl;dr: Use join :: Monad m => m (m a) -> m a since a plain lift will return m (m a) . E.g. write join $ liftM2 f a b But also... liftM s can also be written with Applicative -- e.g. liftM2 a b c == a <$> b <*> c liftM3 a b c d == a <$> b <*> c <*> d etc. In this case, if you're willing to write in that style, you can write it

How to take out a value out of a monad in Haskell?

半城伤御伤魂 提交于 2019-12-18 04:00:39
问题 Is there any way to take "things" out of a monad? I am developing a game, and I am now trying to understand about databases. I found happstack really nice, but I can't get the thing. For example, I have this function (hope you are familiar with happstack ) getAllThings :: MonadIO m => m [Thing] getAllThings = do elems <- query GetThings return elems So I get m [Things] , but I can't use this in my model! For instance doSomeThingWithThings :: [Thing] -> Something I googled this and I found

Why do monads not compose in scala

穿精又带淫゛_ 提交于 2019-12-18 02:16:40
问题 Why do monads not compose when a Monad is an Applicative and an Applicative is a Functor. You see this inheritance chain in many articles on the web ( Which i have gone through ). But when Functors and Applicatives compose why do Monads break this ? Can someone provide a simple example in scala which demonstrates this issue ? I know this is asked a lot but kind of hard to understand without a simple example. 回答1: First, let's start with a simple problem. Let's say, we need to get a sum of two

Why MonadPlus and not Monad + Monoid?

落爺英雄遲暮 提交于 2019-12-17 22:38:12
问题 I'm trying to understand the motivation behind the MonadPlus . Why is it necessary if there are already the typeclasses Monad and Monoid ? Granted, instances of Monoid are concrete types, whereas instances of Monad require a single type parameter. (See Monoid vs MonadPlus for a helpful explanation.) But couldn't you rewrite any type constraint of (MonadPlus m) => ... as a combination of Monad and Monoid ? (Monad m, Monoid (m a)) => ... Take the guard function from Control.Monad , for example.

ST Monad == code smell?

﹥>﹥吖頭↗ 提交于 2019-12-17 21:47:23
问题 I'm working on implementing the UCT algorithm in Haskell, which requires a fair amount of data juggling. Without getting into too much detail, it's a simulation algorithm where, at each "step," a leaf node in the search tree is selected based on some statistical properties, a new child node is constructed at that leaf, and the stats corresponding to the new leaf and all of its ancestors are updated. Given all that juggling, I'm not really sharp enough to figure out how to make the whole

Using return vs. not using return in the list monad

China☆狼群 提交于 2019-12-17 18:13:10
问题 I started my Grand Haskell Crusade (GHC :) ) and I am a bit confused with monads and IO functions. Could anyone explain simply what is the difference between those two functions? f1 = do x <- [1,2] [x, x+1] -- this is monad, right? f2 = do x <- [1,2] return [x, x+1] The results are: *Main> f1 [1,2,2,3] *Main> f2 [[1,2],[2,3]] 回答1: The other answers here are correct, but I wonder if they're not quite what you need... I'll try to keep this as simple as possible, just two points: Point 1. return

Evil use of Maybe monad and extension methods in C#?

北慕城南 提交于 2019-12-17 17:29:45
问题 edit 2015 This question and its answers are no longer relevant. It was asked before the advent of C# 6, which has the null propagating opertor (?.), which obviates the hacky-workarounds discussed in this question and subsequent answers. As of 2015, in C# you should now use Form.ActiveForm?.ActiveControl?.Name. I've been thinking about the null propagation problem in .NET, which often leads to ugly, repeated code like this: Attempt #1 usual code: string activeControlName = null; var activeForm

How and why does the Haskell Cont monad work?

时间秒杀一切 提交于 2019-12-17 17:23:18
问题 This is how the Cont monad is defined: newtype Cont r a = Cont { runCont :: (a -> r) -> r } instance Monad (Cont r) where return a = Cont ($ a) m >>= k = Cont $ \c -> runCont m $ \a -> runCont (k a) c Could you explain how and why this works? What is it doing? 回答1: The first thing to realize about the continuation monad is that, fundamentally, it's not really doing anything at all. It's true! The basic idea of a continuation in general is that it represents the rest of a computation . Say we