Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Monad

Sibi
March 01, 2014

 Monad

Just another abstraction

Sibi

March 01, 2014
Tweet

More Decks by Sibi

Other Decks in Programming

Transcript

  1. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Monad
    March 1, 2014
    Sibi Just Another Abstraction

    View Slide

  2. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    About Me
    Sibi
    Haskell Programmer
    Internet Handle: psibi (github, Twitter)
    Mail: [email protected]
    Sibi Just Another Abstraction

    View Slide

  3. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Outline
    1 Introduction
    2 Monad
    3 Sheep Cloning Experiment
    4 Conclusion
    Sibi Just Another Abstraction

    View Slide

  4. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Some basics
    Types
    TypeClasses
    Functions
    Sibi Just Another Abstraction

    View Slide

  5. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Maybe Type
    For values which may or may not have a value.
    data Maybe a = Just a | Nothing
    Examples:
    Just "hi"
    Just 3
    Nothing
    Sibi Just Another Abstraction

    View Slide

  6. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Definition
    Category theory definition is scary!
    But you don’t need to know that. :-)
    Best learnt with time and observing patterns.
    Sibi Just Another Abstraction

    View Slide

  7. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Monad TypeClass
    class Monad m where
    (>>=) : : m a −> ( a −> m b ) −> m b
    return : : a −> m a
    Sibi Just Another Abstraction

    View Slide

  8. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Maybe Monad
    instance Monad Maybe where
    return x = Just x
    Nothing >>= f = Nothing
    Just x >>= f = f x
    Sibi Just Another Abstraction

    View Slide

  9. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Types and Functions
    type Sheep = . . .
    father : : Sheep −> Maybe Sheep
    father = . . .
    mother : : Sheep −> Maybe Sheep
    mother = . . .
    Sibi Just Another Abstraction

    View Slide

  10. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Other Functions
    maternalGrandfather : : Sheep −> Maybe Sheep
    maternalGrandfather s = case ( mother s ) of
    Nothing −> Nothing
    Just m −> father m
    mpg : : Sheep −> Maybe Sheep
    mpg s = case ( mother s ) of
    Nothing −> Nothing
    Just m −> case ( father m) of
    Nothing −> Nothing
    Just gf −> father gf
    Sibi Just Another Abstraction

    View Slide

  11. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Monads into action!
    mpg : : Sheep −> Maybe Sheep
    mpg s = ( Just s ) >>= mother >>= father >>= father
    Sibi Just Another Abstraction

    View Slide

  12. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Conclusion
    Monads are easy!
    Monads are fun!
    It’s just another abstraction.
    There are lot of other abstractions. :-)
    Sibi Just Another Abstraction

    View Slide

  13. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Courtesy
    All about Monads by Jeff Newbern
    Real World Haskell
    Sibi Just Another Abstraction

    View Slide

  14. Introduction
    Monad
    Sheep Cloning Experiment
    Conclusion
    Questions?
    ???
    Sibi Just Another Abstraction

    View Slide