haskell - Applicative Functor中 `pure`的目的是什么

标签 haskell applicative category-theory

Meet the Applicative typeclass. It lies in the Control.Applicative module and it defines two methods, pure and <*>. It doesn't provide a default implementation for any of them, so we have to define them both if we want something to be an applicative functor.



我试图了解谁在使用 pure功能。我确实使用 (<*>)应用仿函数最有用的函数。但我不确定谁真正使用pure .

我读到了 pure (+3) <*> Just 10 之类的东西但可以写成Just (+3) <*> Just 10也。

以上只是我有太多的困惑之一。定义pure的真正目的是什么?我什么时候可以使用它(或)谁已经在使用它?

最佳答案

<*> :: f (a -> b) -> f a -> f b ,此运算符接受应用类型中的函数,以及应用类型中的值。因此,此运算符的第一个参数不能只是一个函数,而是必须驻留在应用程序中。
pure function 解决了这里可能出现的问题(例如,想要应用一个不在 applicative 中的函数)。它接受一个当前不在应用程序中的函数,并将其提升到应用程序中。 pure :: a -> f a(+3) :: Int -> Int , 和 Just 10 :: Maybe Int , 你不能因此评估 (+3) <*> Just 10因为这些类型不起作用; (+3)必须提升为 Maybe 中的值适用的。

对于 Maybe a , pure 的定义是 pure = Just ,这就是为什么你可以写成 pure (+3)Just (+3)
--

我将留给您查看<$> operator :-) 请记住,每个 Applicative 都是一个 Functor。

关于haskell - Applicative Functor中 `pure`的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51512233/

相关文章:

haskell - 建立 AST 的非法 Monoid 实例不被认为是有害的?

haskell - 如何使用Shake执行sh命令

parsing - 最小纯应用解析器

haskell - Applicative 风格的 Action 之间的通信

haskell - 为什么静态箭头概括了箭头?

c# - Nullable<T> monad 上的绑定(bind)和标识函数在哪里?

haskell - 在 ghci 和 ghc 之间切换时如何防止重新编译

Haskell 函数组合运算符和 'space operator' 优先级

用于类别理论(或图形)图的 javascript 包?

scala - Reader monad - 它如何符合 Monad 接口(interface)?