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 About Me Sibi Haskell

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

    Monad 3 Sheep Cloning Experiment 4 Conclusion Sibi Just Another Abstraction
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Introduction Monad Sheep Cloning Experiment Conclusion Monads into action! mpg

    : : Sheep −> Maybe Sheep mpg s = ( Just s ) >>= mother >>= father >>= father Sibi Just Another Abstraction
  10. 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
  11. Introduction Monad Sheep Cloning Experiment Conclusion Courtesy All about Monads

    by Jeff Newbern Real World Haskell Sibi Just Another Abstraction