用于交换函数参数的函数类型

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)

似乎没有那种对应关系.

那么,这里发生了什么?

bhe*_*ilr 5

这有点令人困惑,但这样看待它

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)

  • OOOOOOOOOHHHHHHHH!我现在明白了!非常感谢.我完全忘记了在等式的右边,我将f应用于z然后x然后y.因此,显然在输入中我以乱七八糟的顺序输入它.谢谢.它完全点击了. (3认同)
  • :) :)和_that_,其他stackoverflow用户,被称为教学和学习发生.干得好. (3认同)