Haskell:为什么((...(.))f g等于f. g x? [英] Haskell: Why is ((.).(.)) f g equal to f . g x?
问题描述
能否请您解释表达式((...(.)))的含义? 据我所知(.)的类型为(b-> c)->(a-> b)-> a-> c.
Could you please explain the meaning of the expression ((.).(.))? As far as I know (.) has the type (b -> c) -> (a -> b) -> a -> c.
推荐答案
(.) . (.)
是组合运算符及其本身的组合.
(.) . (.)
is the composition of the composition operator with itself.
如果我们看
((.) . (.)) f g x
我们可以评估几个步骤,首先我们将其括起来,
we can evaluate that a few steps, first we parenthesise,
((((.) . (.)) f) g) x
然后我们使用(foo . bar) arg = foo (bar arg)
进行申请:
then we apply, using (foo . bar) arg = foo (bar arg)
:
~> (((.) ((.) f)) g) x
~> (((.) f) . g) x
~> ((.) f) (g x)
~> f . g x
更多原则
(.) :: (b -> c) -> (a -> b) -> (a -> c)
因此,使用(.)
作为(.)
的第一个参数,我们必须统一
So, using (.)
as the first argument of (.)
, we must unify
b -> c
使用
(v -> w) -> (u -> v) -> (u -> w)
这产生了
b = v -> w
c = (u -> v) -> (u -> w)
和
(.) (.) = ((.) .) :: (a -> v -> w) -> a -> (u -> v) -> (u -> w)
现在,要将其应用于(.)
,我们必须统一类型
Now, to apply that to (.)
, we must unify the type
a -> v -> w
重命名后
类型为(.)
的
with the type of (.)
, after renaming
(s -> t) -> (r -> s) -> (r -> t)
产生
a = s -> t
v = r -> s
w = r -> t
因此
(.) . (.) :: (s -> t) -> (u -> r -> s) -> (u -> r -> t)
从类型中,我们可以(几乎)看到(.) . (.)
将一个参数的函数应用于两个参数的函数的结果.
and from the type we can (almost) read that (.) . (.)
applies a function (of one argument) to the result of a function of two arguments.
这篇关于Haskell:为什么((...(.))f g等于f. g x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!