monads

Can't understand result of Monad >> application

那年仲夏 提交于 2021-02-08 14:12:13
问题 Operation >> description is the following: Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages. Here is the example which confuses me: > ([1] ++ [2]) >> ([2] ++ [3]) [2,3,2,3] I'm expecting the list [2,3] which would be result of right part of expression. How can result of [2,3,2,3] be explained? 回答1: (>>) is by default defined as a >> b = a >>= (\_ -> b) so the value being ignored is an a in a

Cases in which we shall not use monadic bind to write mfix down using loop

有些话、适合烂在心里 提交于 2021-02-07 13:22:53
问题 I have been trying to write mfix down using Control.Arrow.loop. I came up with different definitions and would like to see which one is mfix 's actual workalike. So, the solution I reckon to be the right one is the following: mfix' :: MonadFix m => (a -> m a) -> m a mfix' k = let f ~(_, d) = sequenceA (d, k d) in (flip runKleisli () . loop . Kleisli) f As one can see, the loop . Kleisli 's argument works for Applicative instances. I find it to be a good sign as we mostly have our knot-tying

Cases in which we shall not use monadic bind to write mfix down using loop

佐手、 提交于 2021-02-07 13:21:11
问题 I have been trying to write mfix down using Control.Arrow.loop. I came up with different definitions and would like to see which one is mfix 's actual workalike. So, the solution I reckon to be the right one is the following: mfix' :: MonadFix m => (a -> m a) -> m a mfix' k = let f ~(_, d) = sequenceA (d, k d) in (flip runKleisli () . loop . Kleisli) f As one can see, the loop . Kleisli 's argument works for Applicative instances. I find it to be a good sign as we mostly have our knot-tying

Are monad laws enforced in Haskell?

懵懂的女人 提交于 2021-02-07 04:53:05
问题 From the Haskell wiki: Monads can be viewed as a standard programming interface to various data or control structures, which is captured by the Monad class. All common monads are members of it: class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a In addition to implementing the class functions, all instances of Monad should obey the following equations, or Monad Laws: return a >>= k = k a m >>= return = m m >>= (\x -> k x >>=

Are monad laws enforced in Haskell?

三世轮回 提交于 2021-02-07 04:53:05
问题 From the Haskell wiki: Monads can be viewed as a standard programming interface to various data or control structures, which is captured by the Monad class. All common monads are members of it: class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a In addition to implementing the class functions, all instances of Monad should obey the following equations, or Monad Laws: return a >>= k = k a m >>= return = m m >>= (\x -> k x >>=

Monads in JavaScript

梦想与她 提交于 2021-02-05 20:20:56
问题 So I want to understand the practical cases where monads in JavaScript are helpful. I read a bunch on articles on Monads in JavaScript and understand that jQuery is one example of its use. But besides the "chaining" pattern, what other issues can be effectively solved using Monads in front-end engineering? Ref: http://importantshock.wordpress.com/2009/01/18/jquery-is-a-monad/ http://igstan.ro/posts/2011-05-02-understanding-monads-with-javascript.html 回答1: Well I think the first article is

How is it possible to collect all error messages in the Either Monad?

核能气质少年 提交于 2021-02-04 19:11:07
问题 I tried to validate the construction of a Record with Applicatives and the Either Monad . It works fine. But I can't see all Error Messages. Only the first is visible because the Right Path of the Either Monad ignores them. Here is my code: import Data.Either (either) import Text.Printf (printf) data Record = Record { fieldA :: String , fieldB :: String , fieldC :: String } deriving (Show, Eq) type Err = String setField :: String -> String -> Either Err String setField field value | length

Class contraints for monads and monad functions

可紊 提交于 2021-02-04 15:08:43
问题 I am trying to write a new monad that only can contain a Num. When it fails, it returns 0 much like the Maybe monad returns Nothing when it fails. Here is what I have so far: data (Num a) => IDnum a = IDnum a instance Monad IDnum where return x = IDnum x IDnum x >>= f = f x fail :: (Num a) => String -> IDnum a fail _ = return 0 Haskell is complaining that there is No instance for (Num a) arising from a use of `IDnum' It suggests that I add a add (Num a) to the context of the type signature

Why can't Haskell be tricked into performing IO operations by using strict evaluation?

有些话、适合烂在心里 提交于 2021-02-04 03:18:28
问题 I'm just learning Haskell and IO monads. I'm wondering why wouldn't this force the program to output "hi" as well as "bye": second a b = b main = print ((second $! ((print "hi") >>= (\r -> return ()))) "bye") As far as I understand, the $! operator would force the first argument of second to be evaluated, and the >>= operator would need to run print "hi" in order to get a value off of it and pass it to \r -> return () , which would print "hi" to the screen. What's wrong with my reasoning? And

Why can't Haskell be tricked into performing IO operations by using strict evaluation?

人走茶凉 提交于 2021-02-04 03:05:14
问题 I'm just learning Haskell and IO monads. I'm wondering why wouldn't this force the program to output "hi" as well as "bye": second a b = b main = print ((second $! ((print "hi") >>= (\r -> return ()))) "bye") As far as I understand, the $! operator would force the first argument of second to be evaluated, and the >>= operator would need to run print "hi" in order to get a value off of it and pass it to \r -> return () , which would print "hi" to the screen. What's wrong with my reasoning? And