The*_*au5 3 haskell types functional-programming
所以我让这些函数交换函数的参数
swap1_3 f x y z = f z y x
toFront3 f x y z = f z x y
Run Code Online (Sandbox Code Playgroud)
这些功能的工作原理如下
foo x y z = [x,y,z]
a = foo 1 2 3 -- returns [1,2,3]
b = swap1_3 foo 1 2 3 -- returns [3,2,1]
c = toFront3 foo 1 2 3 -- returns [3,1,2]
Run Code Online (Sandbox Code Playgroud)
现在,我不明白的是这些函数的类型签名.
类型签名如下
swap1_3 :: (a -> b -> c -> d) -> c -> b -> a -> d
toFront3 :: (a -> b -> c -> d) -> b -> c -> a -> d
Run Code Online (Sandbox Code Playgroud)
从看看
swap1_3
Run Code Online (Sandbox Code Playgroud)
人们会这么认为
a corresponds to the type of x
b corresponds to the type of y
c corresponds to the type of z
d corresponds to the return type of f
Run Code Online (Sandbox Code Playgroud)
但是,当你看到下半部分的类型签名时
toFront3
Run Code Online (Sandbox Code Playgroud)
似乎没有那种对应关系.
那么,这里发生了什么?
这有点令人困惑,但这样看待它
f :: a -> b -> c -> d
f z :: b -> c -> d
f z x :: c -> d
f z x y :: d
Run Code Online (Sandbox Code Playgroud)
这暗示着
z :: a
x :: b
y :: c
Run Code Online (Sandbox Code Playgroud)
所以,我们有
toFront3
:: (a -> b -> c -> d) -- f
-> b -- x
-> c -- y
-> a -- z
toFront3 f x y z = f z x y
Run Code Online (Sandbox Code Playgroud)