Aren't Monads just “conceptual” sugar?

Suppose we enable two types of functions in Haskell:

  • strictly clean (as usual)
  • potentially unclean (procedures)

The distinction will be made by fx announcing that the dot (".") As the first letter of the function name declares it as an unclean procedure.

And then we would set the rules:

  • pure functions can be caused by pure and unclean functions
  • unclean functions can be caused by unclean functions
  • and : unclean functions can be programmed imperatively

With this syntactic sugar and specs on hand - will there still be a need for Monads? Is there anything Monads can do that failed to install above?

B / c, since I came to understand the Monads - this is precisely their goal. It was these very smart people who managed to achieve this exclusively with the help of functional methods with the help of theoretical tools with catheters.

+4
source share
3 answers

No.

Monads have nothing to do with purity or admixture in principle. It so happened that it IObeautifully lubricates the code, but the class Monadcan be used absolutely correctly for cases such as Stateor Maybe, which are absolutely clean.

( ) . "/" - , . , , authorized/unauthorized. ... , ST, STM, RWS, " IO" , .

, .

+16

B/c, - .

/ . , , - , , , .

, Prelude: List, Reader, State, Cont, Either, (->) IO. , IO "" , .

+11

B/c, - .

: http://homepages.inf.ed.ac.uk/wadler/topics/monads.html#monads - Haskell:

1960 , .

, , "" / "" . ( !) - Maybe:

data Maybe a
  = Nothing
  | Just a

instance Monad Maybe where
    return = Just
    Nothing >>= f = Nothing
    Just x >>= f = f x

- (Maybe, liftM, return, join), :

liftM :: (a -> b) -> Maybe a -> Maybe b
liftM f mb = mb >>= return . f

join :: Maybe (Maybe a) -> Maybe a
join Nothing = Nothing
join (Just mb) = mb

, liftM Maybe ( !) Maybe, join Maybe ( Just Just:

join (Just (Just x)) = Just x

while Nothing Nothing :

join Nothing = Nothing
join (Just Nothing) = Nothing

). :

  • Maybe: , .
  • liftM: , , .
  • return: Maybe.
  • join: (, ), , " ".

Maybe - . undef Nothing Just x , x. C/++ t* NULL. Scala : http://www.scala-lang.org/api/current/index.html#scala.Option. "oh, that just exceptions", " ", , , ( Scala foreach). " , " - , Maybe >>=! .

, C Nothing Just Nothing --- . , Haskell, , join , . (, , >>= liftM join, ).

, Haskell IO: GHC IO- GHC -. GHC --- ( !). - . ( ) /!

+4
source

All Articles