monads

Help a C# developer understand: What is a monad?

你离开我真会死。 提交于 2019-11-28 02:31:58
There is a lot of talk about monads these days. I have read a few articles / blog posts, but I can't go far enough with their examples to fully grasp the concept. The reason is that monads are a functional language concept, and thus the examples are in languages I haven't worked with (since I haven't used a functional language in depth). I can't grasp the syntax deeply enough to follow the articles fully ... but I can tell there's something worth understanding there. However, I know C# pretty well, including lambda expressions and other functional features. I know C# only has a subset of

Do statement under a where clause

不想你离开。 提交于 2019-11-28 02:29:36
I'm trying to convert IO [String] to [String] with <- binding; however, I need to use a do block to do that under a where statement, but Haskell complains about the indentation all the time. Here is the code: decompEventBlocks :: IO [String] -> IO [[String]] decompEventBlocks words | words' /= [] = block : (decompEventBlocks . drop $ (length block) words') | otherwise = [] where do words' <- words let block = (takeWhile (/="END") words') What is the reason for that ? And how can we use do block in a where statement ? Moreover, is there any chance that we can have some statements before the

Using Eithers with Scala “for” syntax

人走茶凉 提交于 2019-11-28 02:24:35
问题 As I understand it, Scala "for" syntax is extremely similar to Haskell's monadic "do" syntax. In Scala, "for" syntax is often used for List s and Option s. I'd like to use it with Either s, but the necessary methods are not present in the default imports. for { foo <- Right(1) bar <- Left("nope") } yield (foo + bar) // expected result: Left("nope") // instead I get "error: value flatMap is not a member..." Is this functionality available through some import? There is a slight hitch: for { foo

How to interpret bind/>>= of the function instance?

拟墨画扇 提交于 2019-11-28 01:56:40
I'm trying to improve my understanding of Applicative s and Monad s by implementing their function instances in Javascript. My knowledge of Haskell is limited and I hope that my question makes sense at all. Here are my implementations of fmap , <*> and >>= for the Functor , Applicative and Monad typeclasses in Javascript: const fmap = f => g => x => f(g(x)); // B combinator const apply = f => g => x => f(x) (g(x)); // S combinator const bind = f => g => x => g(f(x)) (x); // ? I am not sure whether bind is the correct translation of the Haskell implementation: (>>=) :: (r -> a) -> (a -> (r -> b

How do you stop building an Option[Collection] upon reaching the first None?

若如初见. 提交于 2019-11-28 00:55:14
问题 When building up a collection inside an Option , each attempt to make the next member of the collection might fail, making the collection as a whole a failure, too. Upon the first failure to make a member, I'd like to give up immediately and return None for the whole collection. What is an idiomatic way to do this in Scala? Here's one approach I've come up with: def findPartByName(name: String): Option[Part] = . . . def allParts(names: Seq[String]): Option[Seq[Part]] = names.foldLeft(Some(Seq

STM monad problem

99封情书 提交于 2019-11-27 23:46:34
问题 This is just a hypothetical scenario to illustrate my question. Suppose that there are two threads and one TVar shared between them. In one thread there is an atomically block that reads the TVar and takes 10s to complete. In another thread is an atomically block that modifies the TVar every second. Will the first atomically block ever complete? Surely it will just keep going back to the beginning, because the log is perpetually in an inconsistent state? 回答1: As others have said: in theory

Why is there no << in the Haskell standard library?

帅比萌擦擦* 提交于 2019-11-27 23:38:41
问题 The Monad class defines a >> method, which sequences two monadic actions: >> :: Monad m => m a -> m b -> m b The binding operator >>= has a flipped-argument equivalent, =<< ; as do the monadic function composition ('fish') operators >=> and <=< . There doesn't seem to be a << , though (after a few minutes of Hoogling). Why is this? Edit: I know it's not a big deal. I just like the way certain lines of code look with the left-pointing operators. x <- doSomething =<< doSomethingElse just looks

Why Functor class has no return function?

半城伤御伤魂 提交于 2019-11-27 22:13:12
From categorical point of view, functor is pair of two maps (one between objects and another between arrows of categories), following some axioms. I have assumed, what every Functor instance is similar to mathematical definition i.e. can map both objects and functions, but Haskell's Functor class has only function fmap which maps functions. Why so? UPD In other words: Every Monad type M has an function return :: a -> M a . And Functor type F has no function return :: a -> F a , but only F x constructor. laughedelic First of all, there are two levels: types and values. As objects of Hask are

Are there contravariant monads?

情到浓时终转凉″ 提交于 2019-11-27 21:34:45
问题 Functors can be covariant and contravariant. Can this covariant/contravariant duality also be applied to monads? Something like: class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b class ContraMonad m where return :: a -> m a contrabind :: m a -> (b -> m a) -> m b Does ContraMonad class make sense? Any examples? 回答1: Well, of course, it's possible to define it, but I doubt it would be of any use. There is a popular saying that "monad is just a monoid in a category of

mtl, transformers, monads-fd, monadLib, and the paradox of choice

╄→гoц情女王★ 提交于 2019-11-27 19:42:20
问题 Hackage has several packages for monad transformers: mtl: Monad transformer library transformers: Concrete functor and monad transformers monads-fd: Monad classes, using functional dependencies monads-tf: Monad classes, using type families monadLib: A collection of monad transformers. mtl-tf: Monad transformer library using type families. mmtl: Modular Monad transformer library mtlx: Monad transformer library with type indexes, providing 'free' copies. compose-trans: Composable monad