applicative

What advantage does Monad give us over an Applicative?

帅比萌擦擦* 提交于 2019-11-27 07:05:07
I've read this article , but didn't understand last section. The author says that Monad gives us context sensitivity, but it's possible to achieve the same result using only an Applicative instance: let maybeAge = (\futureYear birthYear -> if futureYear < birthYear then yearDiff birthYear futureYear else yearDiff futureYear birthYear) <$> (readMay futureYearString) <*> (readMay birthYearString) It's uglier for sure without do-syntax, but beside that I don't see why we need Monad. Can anyone clear this up for me? Here's a couple of functions that use the Monad interface. ifM :: Monad m => m

instance Alternative ZipList in Haskell?

♀尐吖头ヾ 提交于 2019-11-27 06:46:31
问题 ZipList comes with a Functor and an Applicative instance (Control.Applicative) but why not Alternative? Is there no good instance? What about the one proposed below? Is it flawed? is it useless? Are there other reasonable possibilities (like Bool can be a monoid in two ways) and therefore neither should be the instance I searched for "instance Alternative ZipList" (with the quotes to find code first) and only found the library, some tutorials, lecture notes yet no actual instance. Matt

How much is applicative really about applying, rather than “combining”?

旧街凉风 提交于 2019-11-27 06:42:48
问题 For an uncertainty-propagating Approximate type, I'd like to have instances for Functor through Monad . This however doesn't work because I need a vector space structure on the contained types, so it must actually be restricted versions of the classes. As there still doesn't seem to be a standard library for those (or is there? please point me. There's rmonad, but it uses * rather than Constraint as the context kind, which seems just outdated to me), I wrote my own version for the time being.

functions as applicative functors (Haskell / LYAH)

你说的曾经没有我的故事 提交于 2019-11-27 05:19:29
问题 Chapter 11 of Learn You a Haskell introduces the following definition: instance Applicative ((->) r) where pure x = (\_ -> x) f <*> g = \x -> f x (g x) Here, the author engages in some uncharacteristic hand-waving ("The instance implementation for <*> is a bit cryptic, so it's best if we just [show it in action without explaining it]"). I'm hoping someone here might help me figure it out. According to the applicative class definition, (<*>) :: f (a -> b) -> f a -> f b In the instance,

How to interpret bind/>>= of the function instance?

痴心易碎 提交于 2019-11-27 04:49:53
问题 I'm trying to improve my understanding of Applicative s and Monad s by implementing their function instances in Javascript. My knowledge of Haskell is limited and I hope that my question makes sense at all. Here are my implementations of fmap , <*> and >>= for the Functor , Applicative and Monad typeclasses in Javascript: const fmap = f => g => x => f(g(x)); // B combinator const apply = f => g => x => f(x) (g(x)); // S combinator const bind = f => g => x => g(f(x)) (x); // ? I am not sure

Difference between Monad and Applicative in Haskell

丶灬走出姿态 提交于 2019-11-27 02:50:42
I just read the following from typeclassopedia about the difference between Monad and Applicative . I can understand that there is no join in Applicative . But the following description looks vague to me and I couldn't figure out what exactly is meant by "the result" of a monadic computation/action. So, if I put a value into Maybe , which makes a monad, what is the result of this "computation"? Let’s look more closely at the type of (>>=). The basic intuition is that it combines two computations into one larger computation. The first argument, m a, is the first computation. However, it would

When and why should one use Applicative Functors in Scala

自闭症网瘾萝莉.ら 提交于 2019-11-27 02:49:39
I know that Monad can be expressed in Scala as follows: trait Monad[F[_]] { def flatMap[A, B](f: A => F[B]): F[A] => F[B] } I see why it is useful. For example, given two functions: getUserById(userId: Int): Option[User] = ... getPhone(user: User): Option[Phone] = ... I can easily write function getPhoneByUserId(userId: Int) since Option is a monad: def getPhoneByUserId(userId: Int): Option[Phone] = getUserById(userId).flatMap(user => getPhone(user)) ... Now I see Applicative Functor in Scala: trait Applicative[F[_]] { def apply[A, B](f: F[A => B]): F[A] => F[B] } I wonder when I should use it

Distinction between typeclasses MonadPlus, Alternative, and Monoid?

无人久伴 提交于 2019-11-27 02:44:58
The standard-library Haskell typeclasses MonadPlus , Alternative , and Monoid each provide two methods with essentially the same semantics: An empty value: mzero , empty , or mempty . An operator a -> a -> a that joins values in the typeclass together: mplus , <|> , or mappend . All three specify these laws to which instances should adhere: mempty `mappend` x = x x `mappend` mempty = x Thus, it seems the three typeclasses are all providing the same methods. ( Alternative also provides some and many , but their default definitions are usually sufficient, and so they're not too important in

What are Alternative's “some” and “many” useful for?

随声附和 提交于 2019-11-26 19:57:09
问题 Alternative, an extension of Applicative , declares empty , <|> and these two functions: One or more: some :: f a -> f [a] Zero or more: many :: f a -> f [a] If defined, some and many should be the least solutions of the equations: some v = (:) <$> v <*> many v many v = some v <|> pure [] I couldn't find an instance for which some and many are defined. What is their meaning and practical use? Are they used at all? I've been unable to grasp their purpose just from this definition. Update: I'm

What’s an example of a Monad which is an Alternative but not a MonadPlus?

ε祈祈猫儿з 提交于 2019-11-26 19:36:41
问题 In his answer to the question “Distinction between typeclasses MonadPlus, Alternative, and Monoid?”, Edward Kmett says that Moreover, even if Applicative was a superclass of Monad , you’d wind up needing the MonadPlus class anyways, because obeying empty <*> m = empty isn’t strictly enough to prove that empty >>= f = empty So claiming that something is a MonadPlus is stronger than claiming it is Alternative . It’s clear that any applicative functor which is not a monad is automatically an