Examples of using Applicative functors in English and their translations into Vietnamese
{-}
-
Colloquial
-
Ecclesiastic
-
Computer
We have also seen that functions are applicative functors.
Like normal functors, applicative functors come with a few laws.
Lists(actually the list type constructor,[]) are applicative functors.
But with applicative functors, we can apply a function between several functors. .
It turns out there areactually more ways for lists to be applicative functors.
If you try it, you will get stuck, because applicative functors don't allow for the applicative values to interact with each other very much.
But wait,didn't we say that monads are just beefed up applicative functors?
Just like functors have the Functor type class and applicative functors have the Applicative type class, monads come with their own type class: Monad!
So far, we have only used Maybe in our examples andyou might be thinking that applicative functors are all about Maybe.
It may seem like applicative functors are stronger than monads, since applicative functors allow us to take a normal function and make it operate on values with contexts.
Well, there should, but when Haskell was made,it hadn't occured to people that applicative functors are a good fit for Haskell so they weren't in there.
In this chapter, we will take a closer look at functors, along with slightly stronger andmore useful versions of functors called applicative functors.
Because we're alreadyacquainted with how functions work as functors and applicative functors, let's dive right in and see what their Monad instance looks like.
Many times the box analogy is used to help you get some intuition for how functors work, and later,we will probably use the same analogy for applicative functors and monads.
In this chapter, we will learn about monads,which are just beefed up applicative functors, much like applicative functors are only beefed up functors. .
Here's an interesting concept: we can take two applicative functors and combine them into one applicative functor that has inside it the results of those two applicative functors in a list.
Again, see how weused a normal function that takes two strings between two applicative functors of strings just by inserting the appropriate applicative operators.
Then, we learned about improved functors called applicative functors, which allowed us to apply normal functions between several applicative values as well as to take a normal value and put it in some default context.
We saw how monads are stronger than applicatives and functors and how even though all monads are functors and applicative functors, they don't necessarily have Functor and Applicative instances,so we examined the monadic equivalents of the functions that functors and applicative functors use.
Then, we took that concept one step further by introducing applicative functors, which allow us to view values of certain data types as values with contexts and use normal functions on those values while preserving the meaning of those contexts.
If we think about how Maybe acts as an applicative functor, answering this is pretty easy.
When we wrap a value into an applicative functor with pure, the result it yields always has to be that value.
But rest assured, every monad is an applicative functor, even if the Monad class declaration doesn't say so.
Not only is the function type(-gt;) r a functor and an applicative functor, but it's also a monad.
Pure should take a value of any type and return an applicative functor with that value inside it.
We said earlier that it's supposed to take something and wrap it in an applicative functor.
So every monad is an applicative functor and every applicative functor is a functor. .
If we just put a function in a default context and then extract andapply it to a value inside another applicative functor, we did the same as just mapping that function over that applicative functor.
But even though Monad should have the same constraint for Applicative, as every monad is an applicative functor, it doesn't, because the Monad type class was introduced to Haskell way before Applicative. .
Again, from the class definition we see that the f that plays the role of the applicative functor should take one concrete type as a parameter, so we write instance Applicative Maybe where instead of writing instance Applicative(Maybe a) where.