一对如何与箭头函数的类型统一
一些使用箭头的函数在成对使用时非常方便。但我无法理解这些函数的类型如何与一对统一。总的来说,我发现与 Arrow 相关的函数的类型非常混乱。
例如,我们有 first :: abc -> a (b, d) (c, d)
,这对我来说意义不大。但它可以用来增加一对中的第一个数字:
Prelude Control.Arrow> :t first (+1)
first (+1) :: (Num b) => (b, d) -> (b, d)
有人可以
Prelude Control.Arrow> :t (&&&)
(&&&) :: (Arrow a) => a b c -> a b c' -> a b (c, c')
Prelude Control.Arrow> :t (pred &&& succ)
(pred &&& succ) :: (Enum b) => b -> (b, b)
解释一下这是如何工作的吗?
Some of the functions for working with Arrows are quite handy to use on pairs. But I can't understand how the types of these functions unify with a pair. In general, I find the types of the Arrow related functions to be quite confusing.
For example, we have first :: a b c -> a (b, d) (c, d)
, which means little to me. But it can be used to, say, increment the first number in a pair:
Prelude Control.Arrow> :t first (+1)
first (+1) :: (Num b) => (b, d) -> (b, d)
And
Prelude Control.Arrow> :t (&&&)
(&&&) :: (Arrow a) => a b c -> a b c' -> a b (c, c')
Prelude Control.Arrow> :t (pred &&& succ)
(pred &&& succ) :: (Enum b) => b -> (b, b)
Could someone please explain how this works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个
箭头 (->)
的实例。 如此实例化也是
,或者用更传统的符号编写,
其余的应该由此而来。
我在
上一直使用箭头函数(尤其是
实例。我很少将这些组合器用于任何其他(***)
和(&&&)
) >(->)Arrow
实例。因此,每当您看到ab c
时,请考虑“从b
到c
的(广义)函数”,这也适用于常规函数。There is an instance for
Arrow (->)
. Sohas the instantiation
or, written in more conventional notation,
The rest should follow from that.
I use the arrow functions (especially
(***)
and(&&&)
) all the time on the(->)
instance. My usage of those combinators for any other instance ofArrow
is very rare. So whenever you seea b c
, think "(generalized) function fromb
toc
", which works for regular functions too.第一个箭头采用普通箭头,并将其更改为对元组中的第一个元素执行操作,并将结果输出为箭头,
它使用 d 作为元组中未知的第二类型的虚拟变量
&&&接受两个接受相同输入的箭头,并创建一个接受该输入的箭头,将其复制到一个元组中,并在元组的每个部分上运行其中一个箭头,返回更改后的元组。
对于一些扎实的教程,请查看:
http://www.vex.net/~trebla/haskell /hxt-arrow/lesson-0.xhtml
The first arrow takes a normal arrow, and changes it to perform its operation on the first element in a tuple and outputs the result as an arrow
it uses d as a dummy for the unknown second type in the tuple
&&& takes two arrows that take the same input and creates an arrow that takes that input, duplicates it into a tuple and runs one of the arrows on each part of the tuple, returning the altered tuple.
for some solid tutorial, check out:
http://www.vex.net/~trebla/haskell/hxt-arrow/lesson-0.xhtml
我不久前写了一篇关于如何在纯函数上使用箭头函数的博客文章
http://blog.romanandreg.com/post/2755301358/on-how-haskells-are-just-might-just-be-function
我尝试涵盖所有内容以非常简单和详细的方式介绍基本的箭头方法。
干杯。
I did this blog post not long ago about how to use Arrow functions on pure functions
http://blog.romanandreg.com/post/2755301358/on-how-haskells-are-just-might-just-be-function
I try to cover all the basic Arrow methods in a really simple and detailed fashion.
Cheers.