4 ms·
there's an equivalent definition of Applicative that may be a bit less intimidating: class Functor f => Applicative f where unit :: f () (**) :: f a
by uryga 7y ago
there's an equivalent definition of Applicative that may be a bit less intimidating:
class Functor f => Applicative f where
unit :: f ()
(**) :: f a -> f b -> f (a,b)
[source - a bit CT heavy](https://stackoverflow.com/a/35013667/5534735 https://stackoverflow.com/a/35013667/5534735)
(i'll be writing the second operation as `××` in some places because of HN asterisk weirdness)
this gives us:
- `unit`, a "template" container with a hole we can fill (by doing `fmap (const myValue) unit`, equivalent to `pure myValue`)
- `fa ×× fb`, to "compose" two Applicative values. this shows how, unlike with Monads, the two computations must be "independent". for example, if IO were only an Applicative, you could do
print "hello" ** print "world"
but not
getLine >>= \s ->
print ("hello, " ++ s)
(where the second computation depends on the string we got in the first one)
it also nicely shows the two ways lists can be an Applicative - `fa ×× fb` can be either the Cartesian product `[(a,b) | a <- fa, b <- fb]` or `zip a b` (ZipList).
(also, it's kind of like a Monoid, which is neat!)
- TheAsprngHacker 7y agoHuh? I cover this definition in the tutorial, when I discuss OCaml's (and+) operator! Did I gloss over things too quickly?
- uryga 7y agooh man, i must've missed it! tbh though, i just quickly scanned through the article to see if this needed plugging, because I remember being completely stumped by `(<×>)`. too late to edit it now though :/ sorry!