一段是弯曲的结果吗? [英] Is a section the result of currying?

查看:88
本文介绍了一段是弯曲的结果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hutton在Haskell编程中

通常,如果#是运算符,则参数x(#)(x #)(# y)的表达式形式 y被称为节,其含义可以是 使用lambda表达式进行形式化,如下所示:

(#)   =   \x  ->  (\y ->  x   #   y)
(x    #)  =   \y  ->  x   #   y
(#    y)  =   \x  ->  x   #   y

节"和咖喱"之间有什么区别和关系?

部分是将curring操作应用于多参数函数的结果吗?

谢谢.

解决方案

左节和右节是用于将Infix运算符部分应用于单个参数的语法设备(另请参见接受一个(字符串)容器,并测试"apple"是否是它的元素.

关于运算符,例如,当我们写的时候...

5 / 2

...我们将运算符/应用于参数52.运算符也可以以前缀形式使用,而不是使用前缀:

(/) 5 2

以前缀形式,可以按常规方式部分应用运算符:

(/) 5

但是,这看起来有点尴尬-毕竟,5是分子,而不是分母.在这种情况下,我想说的是左节语法更容易:

(5 /)

此外,对第二个参数的部分应用并不十分简单,需要使用lambda或.对于操作员,右侧部分可以帮助您解决此问题:

(/ 2)

请注意,各节还可以通过反引号语法使用运算符中的函数,因此...

(`elem` ["apple", "grape", "orange"])

...接受一个字符串,并测试是否可以在["apple", "grape", "orange"]中找到它.

In Programming in Haskell by Hutton

In general, if # is an operator, then expressions of the form (#), (x #), and (# y) for arguments x and y are called sections, whose meaning as functions can be formalised using lambda expressions as follows:

(#)   =   \x  ->  (\y ->  x   #   y)
(x    #)  =   \y  ->  x   #   y
(#    y)  =   \x  ->  x   #   y

What are the difference and relation between "section" and "currying"?

Is a section the result of applying the currying operation to a multi-argument function?

Thanks.

解决方案

Left sections and right sections are syntactical devices for partially applying an infix operator to a single argument (see also chepner's answer). For the sake of accuracy, we should note that currying is not the same thing as partial application:

  • Currying is converting a function that takes N arguments into a function that takes a single argument and returns a function that takes N-1 arguments.

  • Partial application is making a function that takes N-1 arguments out of a function that takes N arguments by supplying one of the arguments.

In Haskell, it happens that everything is curried; all functions take just one argument (even uncurried functions in Haskell take a tuple, which is, strictly speaking, a single argument -- you might want to play with the curry and uncurry functions to see how that works). Still, we very often think informally of functions that return functions as functions of multiple arguments. From that vantage point, a nice consequence of currying by default is that partial application of a function to its first argument becomes trivial: while, for instance, elem takes a value and a container and tests if the value is an element of the contaier, elem "apple" takes a container (of strings) and tests if "apple" is an element of it.

As for operators, when we write, for instance...

5 / 2

... we are applying the operator / to the arguments 5 and 2. The operator can also be used in prefix form, rather than infix:

(/) 5 2

In prefix form, the operator can be partially applied in the usual way:

(/) 5

That, however, arguably looks a little awkward -- after all, 5 here is the numerator, and not the denominator. I'd say left section syntax is easier on the eye in this case:

(5 /)

Furthermore, partial application to the second argument is not quite as straightforward to write, requiring a lambda, or flip. In the case of operators, a right section can help with that:

(/ 2)

Note that sections also work with functions made into operators through backtick syntax, so this...

(`elem` ["apple", "grape", "orange"])

... takes a string and tests whether it can be found in ["apple", "grape", "orange"].

这篇关于一段是弯曲的结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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