一对如何与箭头函数的类型统一

发布于 2024-10-14 12:08:19 字数 584 浏览 3 评论 0原文

一些使用箭头的函数在成对使用时非常方便。但我无法理解这些函数的类型如何与一对统一。总的来说,我发现与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩于岁月 2024-10-21 12:08:19

有一个箭头 (->) 的实例。 如此

(&&&) :: (Arrow a) => a b c -> a b c' -> a b (c,c')

实例化也是

(&&&) :: (->) b c -> (->) b c' -> (->) b (c,c')

,或者用更传统的符号编写,

(&&&) :: (b -> c) -> (b -> c') -> (b -> (c,c'))

其余的应该由此而来。

我在 一直使用箭头函数(尤其是 (***)(&&&)) >(->) 实例。我很少将这些组合器用于任何其他 Arrow 实例。因此,每当您看到 ab c 时,请考虑“从 bc 的(广义)函数”,这也适用于常规函数。

There is an instance for Arrow (->). So

(&&&) :: (Arrow a) => a b c -> a b c' -> a b (c,c')

has the instantiation

(&&&) :: (->) b c -> (->) b c' -> (->) b (c,c')

or, written in more conventional notation,

(&&&) :: (b -> c) -> (b -> c') -> (b -> (c,c'))

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 of Arrow is very rare. So whenever you see a b c, think "(generalized) function from b to c", which works for regular functions too.

饮惑 2024-10-21 12:08:19

第一个箭头采用普通箭头,并将其更改为对元组中的第一个元素执行操作,并将结果输出为箭头,

a b c -> a (b, d) (c, d)

a b c -- is the input arrow, an operation that maps type b to c
a (b, d) (c, d) -- is the output arrow, an operation that maps a tuple (b, d) to (c, d)

它使用 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

a b c -> a (b, d) (c, d)

a b c -- is the input arrow, an operation that maps type b to c
a (b, d) (c, d) -- is the output arrow, an operation that maps a tuple (b, d) to (c, d)

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

遥远的她 2024-10-21 12:08:19

我不久前写了一篇关于如何在纯函数上使用箭头函数的博客文章

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文