monads

How to enumerate a recursive datatype in Haskell?

北慕城南 提交于 2019-11-26 14:08:54
问题 This blog post has an interesting explanation of how to use the Omega monad to enumerate an arbitrary grammar diagonally. He offers an example of how to do so, resulting in an infinite sequence of strings. I'd like to do the same, except that, instead of generating a list of strings, it generates a list of an actual datatype. For example, data T = A | B T | C T T Would generate A, B A, C A A, C (B A) A... Or something similar. Unfortunately my Haskell skills are still maturing and after some

Converting IO Int to Int

為{幸葍}努か 提交于 2019-11-26 14:08:26
问题 I've created a combobox from converting a xmlWidget to a comboBox with the function castTocomboBox and now I want to get the text or the index of the active item. The problem is that if I use the comboBoxGetActive function it returns an IO Int result and I need to know how can I obtain the Int value. I tried to read about monads so I could understand what one could do in a situation like this but I don't seem to understand. I appreciate all the help I can get. I should probably mention that I

What advantage does Monad give us over an Applicative?

情到浓时终转凉″ 提交于 2019-11-26 12:58:51
问题 I\'ve read this article, but didn\'t understand last section. The author says that Monad gives us context sensitivity, but it\'s possible to achieve the same result using only an Applicative instance: let maybeAge = (\\futureYear birthYear -> if futureYear < birthYear then yearDiff birthYear futureYear else yearDiff futureYear birthYear) <$> (readMay futureYearString) <*> (readMay birthYearString) It\'s uglier for sure without do-syntax, but beside that I don\'t see why we need Monad. Can

Euler 43 - is there a monad to help write this list comprehension?

你离开我真会死。 提交于 2019-11-26 12:47:27
问题 Here is a way to solve Euler problem 43 (please let me know if this doesn\'t give the correct answer). Is there a monad or some other syntatic sugar which could assist with keeping track of the notElem conditions? toNum xs = foldl (\\s d -> s*10+d) 0 xs numTest xs m = (toNum xs) `mod` m == 0 pandigitals = [ [d0,d1,d2,d3,d4,d5,d6,d7,d8,d9] | d7 <- [0..9], d8 <- [0..9], d8 `notElem` [d7], d9 <- [0..9], d9 `notElem` [d8,d7], numTest [d7,d8,d9] 17, d5 <- [0,5], d5 `notElem` [d9,d8,d7], d3 <- [0,2

What is the purpose of the reader monad?

我怕爱的太早我们不能终老 提交于 2019-11-26 12:34:37
问题 The reader monad is so complex and seems to be useless. In an imperative language like Java or C++, there is no equivalent concept for the reader monad, if I am not mistaken. Can you give me a simple example and clear this up a little bit? 回答1: Don't be scared! The reader monad is actually not so complicated, and has real easy-to-use utility. There are two ways of approaching a monad: we can ask What does the monad do ? What operations is it equipped with? What is it good for? How is the

Why are side-effects modeled as monads in Haskell?

我怕爱的太早我们不能终老 提交于 2019-11-26 11:43:42
问题 Could anyone give some pointers on why the impure computations in Haskell are modelled as monads? I mean monad is just an interface with 4 operations, so what was the reasoning to modelling side-effects in it? 回答1: Suppose a function has side effects. If we take all the effects it produces as the input and output parameters, then the function is pure to the outside world. So for an impure function f' :: Int -> Int we add the RealWorld to the consideration f :: Int -> RealWorld -> (Int,

Method parameters validation in Scala, with for comprehension and monads

六月ゝ 毕业季﹏ 提交于 2019-11-26 11:12:31
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).toRight("Name is mandatory for a normal category").right errors : Option[String] <- Option(description)

Why are Promises Monads?

天大地大妈咪最大 提交于 2019-11-26 11:02:28
问题 I\'ve been learning about functional programming and have come across Monads, Functors and Applicatives. From my understanding the following definitions apply: a) ( A=>B ) => C[A] => C[B] | Functor b) ( A=>C[B] ) => C[A] => C[B] | Monad c) ( C[A=>B] ) => C[A] => C[B] | Applicative (reference: https://thedet.wordpress.com/2012/04/28/functors-monads-applicatives-can-be-so-simple/) Furthermore, I understand a Monad is a special case of a Functor. As in, it applies a function that returns a

Difference between Monad and Applicative in Haskell

我与影子孤独终老i 提交于 2019-11-26 10:16:51
问题 I just read the following from typeclassopedia about the difference between Monad and Applicative . I can understand that there is no join in Applicative . But the following description looks vague to me and I couldn\'t figure out what exactly is meant by \"the result\" of a monadic computation/action. So, if I put a value into Maybe , which makes a monad, what is the result of this \"computation\"? Let’s look more closely at the type of (>>=). The basic intuition is that it combines two

Applicatives compose, monads don&#39;t

岁酱吖の 提交于 2019-11-26 09:47:43
Applicatives compose, monads don't. What does the above statement mean? And when is one preferable to other? 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 such interference: the function and argument computations don't depend on values. This really bites. Compare