简体   繁体   English

Haskell中翻转function的定义

[英]Definition of the flip function in Haskell

Currently I am trying to learn Haskell with the book 'Learn You a Haskell' and I'm trying to understand the implementations of the flip function in chapter 5 .目前我正在尝试通过“Learn You a Haskell”一书来学习 Haskell,并且我正在尝试了解第 5 章flip function 的实现。 The problem is that the author states that if gxy = fyx is valid, then fyx = gxy must be also true.问题是作者声明如果gxy = fyx有效,那么fyx = gxy也必须为真。 But how and why does this reversal affects the two function definitions?但是这种反转如何以及为什么会影响两个 function 定义?

I know how currying works and I also know that the -> operator is right-associative by default so the type declarations are in fact the same.我知道柯里化是如何工作的,而且我也知道->运算符默认是右关联的,因此类型声明实际上是相同的。 I also understand the functions apart from each other but not how the reversal of gxy = fyx is in relation with this.我也了解彼此分开的功能,但不了解gxy = fyx的反转与此有何关系。

first flip function第一次翻转 function

flip' :: (a -> b -> c) -> (b -> a -> c)
flip' f = g
    where g x y = f y x

second flip function第二次翻转 function

flip' :: (a -> b -> c) -> b -> a -> c
flip' f y x = f x y

I think the argument the author has in his head got wildly abbreviated to the point that it's not even sensical. 我认为作者脑海中的论点被粗略地缩写为一点都不合理。 But here is my guess about the reasoning. 但是这是我对推理的猜测。 We start with the first definition: 我们从第一个定义开始:

flip f = g where g x y = f y x

Now we observe that this is a curried thing, and we use all the discussion of associativity of (->) and junk to write the same thing, but with two extra arguments to f . 现在我们观察到这是一个可咖喱的东西,并且我们使用(->)和垃圾的所有关联性讨论来写相同的东西,但是对f附加了两个参数。 Like this: 像这样:

flip f x y = g x y where g x y = f y x

Now we have the equation that he called out as going both ways: gxy = fyx and vice versa. 现在我们得到了他双向调用的方程: gxy = fyx ,反之亦然。 We can rewrite the body of the flip using this equation, like this: 我们可以使用以下公式重写flip的主体,如下所示:

flip f x y = f y x where g x y = f y x

Since the body of the definition no longer mentions g , we can delete it. 由于定义的主体不再提及g ,因此我们可以将其删除。

flip f x y = f y x

And now we're pretty much there. 现在我们几乎在那里。 In the final definition, the author has swapped the names x and y everywhere. 在最终定义中,作者在各处交换了名称xy I don't know why they chose to do so, but it is a legal move that you can make in equational reasoning, so no problem there. 我不知道他们为什么选择这么做,但这是您可以在方程式推理中做出的合法举动,所以在那里没有问题。 Doing so gives us their final equation: 这样做为我们提供了最终方程:

flip f y x = f x y

flip takes a function and returns the flipped version of the function. flip接受一个函数并返回该函数的翻转版本。 One way to define flip' is to include the application in the definition itself, since 定义“ flip'一种方法是将应用程序包含在定义本身中,因为

flip' f y x = f x y  ===> flip' f y = \x -> f x y
                     ===> flip' f = \y -> \x -> f x y

That is, flip' f is a function that applies f to its own arguments in reverse order. 也就是说, flip' f是函数,它以相反的顺序将f应用于自己的参数。

The second definition simply gives the anonymous function \\y -> \\x -> fxy a name first, then uses that name as the definition of flip' fxy . 第二个定义只是简单地给匿名函数\\y -> \\x -> fxy一个名称,然后使用该名称作为flip' fxy的定义。

                     ===> flip' f = g where g = \y -> \x -> f x y
                     ===> flip' f = g where g y = \x -> f x y
                     ===> flip' f = g where g y x = f x y

That is, flip' f is some function g , where g is defined to apply f to the arguments of g in reverse order. 即, flip' f是一些功能g ,其中g被定义为应用f到的参数g以相反的顺序。

The definitions gxy = fyx and gyx = fxy are equivalent up to alpha conversion . 定义gxy = fyxgyx = fxy等效于alpha转换 In both cases, f is a free variable in the definition of g ; 在这两种情况下, fg的定义中都是自由变量; g is as a closure over the f argument to flip' . gf flip'f参数的闭包。

If you are here looking for what flip does:如果您在这里寻找翻转的作用:

Prelude> pow x y = x ^ y
Prelude> pow 2 3
8
Prelude> flip pow 2 3
9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM