Why is>> = more often said than <= <?

Say we have g :: a -> band f :: b -> c. We can write:

  • f . g :: a -> c.

If our functions return monadic values ​​(i.e. values ​​in contexts), for example, g1 :: (Monad m) => a -> m band f1 :: (Monad m) => b -> m c. We can write:

  • f1 <=< g1 :: (Monad m) => a -> m c.
  • return x >>= g1 >>= f1where x :: ato return the value. Or even lambda \x -> return x >>= g1 >>= f1.

It seems to be <=<more parallel .in terms of syntax. <=<simplifies understanding Monad- it's just a composite function that preserves context. Why >>=more often say than <=<?

+6
source share
2 answers

<=<- A great way to explain the laws of the monad :

 f <=< return = f -- right identity
 return <=< g = g -- left identity
 f <=< (g <=< h) = (f <=< g) <=< h -- associativity

Kleisli:

 newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
 instance Monad m => Category (Kleisli m) where
   Kleisli f . Kleisli g = Kleisli (f <=< g)
   id = Kleisli return

, . =<<.

, , , >>= Haskell.

, , <=< - , , C- (C, ++, Java, Python, ..), .

"point-free" - , :

f a b = a + b * 2
f a = (a +) . (* 2)
f = flip (.) (*2) . (+)

, - , , eta conversion.

, , .

, , , " IO String, String?" Haskell IO monad. , , , " , >>= ", >>=,

putStrLn "Your first name: " >>= \_ ->
getLine >>= \first ->
putStrLn "Your last name: " >>= \_ ->
getLine >>= \last ->
putStrLn ("Hello " ++ first ++ " " ++ last)

do
  putStrLn "Your first name: "
  first <- getLine
  putStrLn "Your last name: "
  last <- getLine
  putStrLn ("Hello " ++ first ++ " " ++ last)

, , , >>= Monad, <=< , , . , , , ( -).

+9

<=< , >>=, Monad. , , f :: a -> m b g :: b -> m c, - f, , >>=!

, , Maybe

m >>= f = case m of
    Nothing -> Nothing
    Just a -> f a

f >=> g = \a -> case f a of
    Nothing -> Nothing
    Just b -> g b

>=>, !

, >>= >=>, :

(>>= f) = id >=> f
f >=> g = \a -> f a >>= g
-- equivalently = (>>= g) . (>>= f) . return

, >=> " ", >>=, Monad, .

, \m -> m >>= f - id >=> f. , >=> .

( , =<< , -, $, <$> <*>, )

+1

All Articles