Skip to content

Instantly share code, notes, and snippets.

@dtoma
Created March 26, 2019 08:36
Show Gist options
  • Save dtoma/46760eb50d2424c06297434184bbfa29 to your computer and use it in GitHub Desktop.
Save dtoma/46760eb50d2424c06297434184bbfa29 to your computer and use it in GitHub Desktop.
Typeclasses notes

We can use an Applicative all the way:

Prelude> pure (*) <*> (Just 2) <*> (Just 3)
Just 6

But we can also use a Functor and then Applicative for the rest of the computation:

Prelude> (*) <$> (Just 2) <*> (Just 3)
Just 6

From Typeclassopedia:

Why do we need Applicative to implement this generalization of fmap? Suppose we use fmap to apply g to the first parameter x1. Then we get something of type f (t2 -> ... t), but now we are stuck: we can’t apply this function-in-a-context to the next argument with fmap. However, this is precisely what (<*>) allows us to do. This suggests the proper translation of the idealized notation [[g \; x_1 \; x_2 \; \cdots \; x_n]]\ into Haskell, namely g <$> x1 <*> x2 <*> ... <*> xn,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment