Understand Functor, Applicative and Monad

in #haskell6 years ago (edited)

Functor

  • A Functor is any data type that defines how fmap applies to it.
  • Lists are functors too!
  • Functions are Functors too. When you use fmap on a function, you're just doing function composition!
  • Functors apply a function to a wrapped value

Applicatives

  • Applicatives take it to the next level. With an applicative, our values are wrapped in a context.
  • Applicative pushes Functor aside.
  • Control.Applicative defines <*>
  • Applicatives apply a wrapped function to a wrapped value

Monads

  • Monads apply a function that returns a wrapped value to a wrapped value.
  • Monads have a function >>= (pronounced "bind") to do this.
  • Monad is another typeclass.
  • Maybe is a Functor, an Applicative, and a Monad
  • Haskell also provides us with some syntactical sugar for monads, called do notation

Conclusion

  • Functor, Applicative, Monad are typeclass, the value implements them is one Functor or Applicative or Monad
  • A functor is a data type that implements the Functor typeclass.
  • An applicative is a data type that implements the Applicative typeclass.
  • A monad is a data type that implements the Monad typeclass.
  • A Maybe implements all three, so it is a functor, an applicative, and a monad.
  • syntax sugar
    1. functors: you apply a function to a wrapped value using fmap or <$>
    2. applicatives: you apply a wrapped function to a wrapped value using <*> or liftA
    3. monads: you apply a function that returns a wrapped value, to a wrapped value using >>= or liftM

# normal
f a -> b
# functor: <a>
f <a> -> <b>
# applicative: <b>
<f> <a> -> <b>
# monad: f a -> <b>
<a> (f a -> <b>) -> <b>

所以

  • functor 解包参数
  • applicative 解包函数
  • monad 把一个既不是normal也不是functor情形的函数和一个包裹参数颠倒顺序

另外一种视角

  1. Functor: apply a function to a container.
  2. Applicative: apply a multi-argument function to multiple containers.
  3. Monad: like Applicative but I can decide what to do next after each step.

所以可以把这个 container 看作“更多数量的元素”,把monad看做可中断的流水线。

Links

Sort:  

Congratulations @weaming! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post
You made your First Vote
You got a First Vote

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:
SteemitBoard and the Veterans on Steemit - The First Community Badge.

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @weaming! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

New japanese speaking community Steem Meetup badge
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.17
TRX 0.13
JST 0.027
BTC 60450.32
ETH 2604.48
USDT 1.00
SBD 2.60