我如何理解“(。)。 (。)&QUOT ;? [英] How can I understand "(.) . (.)"?

查看:110
本文介绍了我如何理解“(。)。 (。)&QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我了解 fmap。

I believe I understand fmap . fmap for Functors, but on functions it's hurting my head for months now.

我已经看到你可以应用的函数来定义函数,但函数会伤害我的头几个月。 code>(。)(。)。 (。),但我忘记了如何做到这一点。

当我自己尝试时,它总是出错:

I've seen that you can just apply the definition of (.) to (.) . (.), but I've forgot how to do that.
When I try it myself it always turns out wrong:

(.) f g = \x -> f (g x)
(.) (.) (.) = \x -> (.) ((.) x)
\x f -> (.) ((.) x) f
\x f y  -> (((.)(f y)) x)
\x f y g-> (((.)(f y) g) x)
\x f y g-> ((f (g y)) x)
\x f y g-> ((f (g y)) x):: t2 -> (t1 -> t2 -> t) -> t3 -> (t3 -> t1) -> t

如果只应用定义是唯一的方法,那么怎么会有人出现与(。)。 (。)

必须有一些更深入的理解或直觉,我错过了。

If "just applying the definition" is the only way of doing it, how did anybody come up with (.) . (.)?
There must be some deeper understanding or intuition I'm missing.

推荐答案

您也可以使用您对 fmap的理解。 fmap

如果您有两个 Functor s foo 和 bar ,然后

If you have two Functors foo and bar, then

fmap . fmap :: (a -> b) -> foo (bar a) -> foo (bar b)

fmap。 fmap 接受一个函数并为这两个 Functor s的组合产生一个诱导函数。

fmap . fmap takes a function and produces an induced function for the composition of the two Functors.

现在,对于任何类型 t ( - >)t Functor 以及 Functor fmap (。 )

Now, for any type t, (->) t is a Functor, and the fmap for that Functor is (.).

所以(。)。 (。) fmap。对于两个 Functor s <( - >)s 和fmap ( - >)t ,因此

So (.) . (.) is fmap . fmap for the case where the two Functors are (->) s and (->) t, and thus

(.) . (.) :: (a -> b) -> ((->) s) ((->) t a) -> ((->) s) ((->) t b)
          =  (a -> b) -> (s -> (t -> a))     -> (s -> (t -> b))
          =  (a -> b) -> (s -> t -> a)       -> (s -> t -> b)

它合成一个函数 f :: a - > b 带有两个参数的函数, g :: s - > t - >一个

it "composes" a function f :: a -> b with a function of two arguments, g :: s -> t -> a,

((.) . (.)) f g = \x y -> f (g x y)

该观点也清楚表明,该模式如何扩展到函数更多参数,

That view also makes it clear that, and how, the pattern extends to functions taking more arguments,

(.) . (.) . (.) :: (a -> b) -> (s -> t -> u -> a) -> (s -> t -> u -> b)

等。

这篇关于我如何理解“(。)。 (。)&QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆