monads

What is the difference between a monad and a closure?

社会主义新天地 提交于 2020-01-01 02:43:29
问题 i am kinda confused reading the definition between the two. Can they actually intersect in terms of definition? or am i completely lost? Thanks. 回答1: Closures, as the word tends to be used, are just functions (or blocks of code, if you like) that you can treat like a piece of data and pass to other functions, etc. (the "closed" bit is that wherever you eventually call it, it behaves just as it would if you called it where it was originally defined). A monad is (roughly) more like a context in

Is a collection with flatMap a monad?

别等时光非礼了梦想. 提交于 2020-01-01 02:16:13
问题 Scala has the trait Iterable[A] that defines def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): Iterable[B] That certainly looks like the bind function on a monad, and the documentation hints that it is a monad, but there are two objections, one minor and one major: the minor one: the return-type of the function passed in is this GenTraversableOnce . I think this is just a convenience that can be overlooked when judging monad-ness. the major one: the "value" of the monad is the list of all the

What is MonadBaseControl for?

我的未来我决定 提交于 2020-01-01 01:48:06
问题 I'm digging deeper into Yesod's monads, and have encountered MonadBaseControl . I took a look at the hackage doc, and got lost. Could someone tell me the problem it is trying to solve? 回答1: It comes from the package monad-control, and is one of a pair of type classes (the other one being MonadTransControl) that enhance MonadBase (resp. MonadTrans) by supporting an alternative liftBase (resp. lift ) operation for monads that implement it. This enhanced version no longer takes a simple action

F#: Is there a way to extend the monad keyword list?

北城余情 提交于 2019-12-31 08:11:40
问题 Inside an F# monad, if you say let! , the compiler translates that to a Bind member that you've defined on the monad builder. Now I see there are Query monads, as shown here on MSDN, where you can say: query { for student in db.Student do select student count } and the select and count , for example, will be translated to the QueryBuilder members Linq.QueryBuilder.Select and Linq.QueryBuilder.Count. My question is, is this mapping of keywords to members hardwired into the F# compiler, or is

Has anyone ever encountered a Monad Transformer in the wild?

北城余情 提交于 2019-12-31 08:07:31
问题 In my area of business - back office IT for a financial institution - it is very common for a software component to carry a global configuration around, to log its progress, to have some kind of error handling / computation short circuit... Things that can be modelled nicely by Reader-, Writer-, Maybe-monads and the like in Haskell and composed together with monad transformers. But there seem to some drawbacks: The concept behind monad transformers is quite tricky and hard to understand,

Explanation of Monad laws

非 Y 不嫁゛ 提交于 2019-12-31 08:05:20
问题 From a gentle introduction to Haskell, there are the following monad laws. Can anyone intuitively explain what they mean? return a >>= k = k a m >>= return = m xs >>= return . f = fmap f xs m >>= (\x -> k x >>= h) = (m >>= k) >>= h Here is my attempted explanation: We expect the return function to wrap a so that its monadic nature is trivial. When we bind it to a function, there are no monadic effects, it should just pass a to the function. The unwrapped output of m is passed to return that

Can I use different workflows simultaneously in F#?

独自空忆成欢 提交于 2019-12-31 04:37:08
问题 I need my state to be passed along while being able to chain functions with the maybe workflow. Is there a way for 2 workflows to share the same context? If no, what is the way of doing it? UPDATE: Well, I have a state that represents a segment of available ID's for the entities that I am going to create in the database. So once an ID is acquired the state has to be transformed to a newer state with the next available ID and thrown away so that nobody can use it again. I don't want to mutate

MonadError section in “All about monads”

大城市里の小女人 提交于 2019-12-31 03:17:10
问题 I'm now really confused about the Error monad in which "All about monads" describes. It claims the definition of Error monad as class (Monad m) => Monaderror e m | m -> e where throwError :: e -> m a catchError :: m a -> (e -> m a) -> m a And one of the instance is Either e. instance MonadError (Either e) where throwError = Left (Left e) `catchError` handler = handler e a `catchError` _ = a Here is what I don't understand. The MonadError class take two type parameters, and (Either e) takes

Monads, composition and the order of computation

喜欢而已 提交于 2019-12-31 01:25:33
问题 All the monad articles often state, that monads allow you to sequence effects in order. But what about simple composition? Ain't f x = x + 1 g x = x * 2 result = f g x requires g x to be computed before f ... ? Do monads do the same but with handling of effects? 回答1: Disclaimer : Monads are a lot of things. They are notoriously difficult to explain, so I will not attempt to explain what monads are in general here, since the question does not ask for that. I will assume you have a basic grasp

Convert a “do” notation with more than two actions to use the bind function

主宰稳场 提交于 2019-12-30 18:42:07
问题 I know that the following "do" notation's "bind" function is equivalent to getLine >>= \line -> putStrLn do line <- getLine putStrLn line But how is the following notation equivalent to bind function? do line1 <- getLine putStrLn "enter second line" line2 <- getLine return (line1,line2) 回答1: I take it you are trying to see how to bind the result of "putStrLn". The answer is in the type of putStrLn: putStrLn :: String -> IO () Remember that "()" is the unit type, which has a single value (also