在 Haskell 中研究 functors、applicative functors 和 monads,我在 Wikipedia 上找到了这个定义。 :
In functional programming, specifically Haskell, an applicative functor is a structure that is like a monad (
return
,fmap
,join
) withoutjoin
, or like a functor withreturn
.
我无法理解:在我看来,提供
return
(即 pure
)到仿函数不足以获得应用仿函数,因为您需要提供 ap
(即 <*>
),不能用 fmap
定义和 return
只要。我错过了什么还是维基百科的定义不是绝对正确的?编辑 2017-02-08:我在 this 中发现了有关此问题的其他有用见解回答。
最佳答案
文章不正确。假设我们有一个单子(monad) m
没有 join
, 或带有 return
的仿函数.我们可以定义pure
立即地:
pure :: Monad m => a -> m a
pure = return
然而,我们不能定义
(<*>)
与 fmap
和 return
只要。我们只有 fmap
,所以我们最终会得到 m (m a)
如果我们尝试使用 m (a -> b)
.那时我们需要join
或其等价物(>>=)
:(<*>) :: Monad m => m (a -> b) -> m a -> m b
f <*> x = join (fmap (flip fmap x) f)
-- or, easier to read:
-- f <*> x = do
-- f' <- f
-- x' <- x
-- return f' x'
应用仿函数就像具有
return
的仿函数。和 ap
,但没有 join
.所以是的,你完全正确,维基百科错过了 applicative 的操作(见 the original paper )。顺便说一句,如果你只添加
pure
, 你得到一个 pointed functor . typeclassopedia提供对 Applicative
的更好概述不过,比维基百科的文章。
关于haskell - 我无法理解维基百科对 "applicative functor"的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42043385/