部分应用的功能 [英] Partially applied functions

查看:48
本文介绍了部分应用的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习函数编程时,部分应用函数的概念出现了很多.在Haskell中,像内置函数 take 之类的东西被认为是部分应用的.

While studying functional programming, the concept partially applied functions comes up a lot. In Haskell, something like the built-in function take is considered to be partially applied.

对于部分应用功能或使用/暗示它到底意味着什么,我仍然不清楚.

I am still unclear as to what it exactly means for a function to be partially applied or the use/implication of it.

推荐答案

严格来说,部分应用程序是指这样一种情况:您为函数提供的参数少于预期的数量,并且返回了动态创建的新函数,该函数期望剩下的参数.

Strictly speaking, partial application refers to a situation where you supply fewer arguments than expected to a function, and get back a new function created on the fly that expects the remaining arguments.

严格来说,这在Haskell中也不适用,因为每个函数都精确地使用 一个参数.没有部分应用程序,因为您可以将函数应用于它的参数,也可以不应用于它.

Also strictly speaking, this does not apply in Haskell, because every function takes exactly one argument. There is no partial application, because you either apply the function to its argument or you don't.

但是,Haskell提供了语法糖来定义函数,包括模拟多参数函数.因此,在Haskell中,部分应用程序"是指提供的数量少于获得无法进一步应用于另一个参数的值所需的参数的总数.使用每个人最喜欢的 add 示例

However, Haskell provides syntactic sugar for defining functions that includes emulating multi-argument functions. In Haskell, then, "partial application" refers to supplying fewer than the full number of arguments needed to obtain a value that cannot be further applied to another argument. Using everyone's favorite add example,

add :: Int -> Int -> Int
add x y = x + y

该类型表示 add 接受一个类型为 Int 的参数,并返回一个类型为 Int->的函数.Int .-> 类型的构造函数是右关联的,因此它有助于显式地将其括起来以强调Haskell函数的一参数性质: Int->(整数->整数).

the type indicates that add takes one argument of type Int and returns a function of type Int -> Int. The -> type constructor is right-associative, so it helps to explicitly parenthesize it to emphasize the one-argument nature of a Haskell function: Int -> (Int -> Int).

调用这样的多参数"函数时,我们利用了该函数应用程序是 left 关联的事实,因此我们可以编写 add3 5 代替(加3)5 .调用 add 3 被认为是部分应用,因为我们可以进一步将结果应用于另一个参数.

When calling such a "multi-argument" function, we take advantage of the fact the function application is left-associative, so we can write add 3 5 instead of (add 3) 5. The call add 3 is thought of as partial application, because we could further apply the result to another argument.

我提到了Haskell提供的语法糖,以简化复杂的高阶函数的定义.定义函数的一种基本方法是:使用lambda表达式.例如,要定义一个将参数加3的函数,我们编写

I mentioned the syntactic sugar Haskell provides to ease defining complex higher-order functions. There is one fundamental way to define a function: using a lambda expression. For example, to define a function that adds 3 to its argument, we write

\x -> x + 3

为便于参考,我们可以为此函数分配一个名称:

For ease of reference, we can assign a name to this function:

add = \x -> x + 3

为进一步简化定义,Haskell让我们在其位置书写

To further ease the defintion, Haskell lets us write in its place

add x = x + 3

隐藏显式lambda表达式.

hiding the explicit lambda expression.

对于高阶多参数"函数,我们将编写一个函数将两个值相加为

For higher-order, "multiargument" functions, we would write a function to add two values as

\x -> \y -> x + y

要隐藏隐藏的内容,我们可以改写

To hide the currying, we can write instead

\x y -> x + y.

结合用参数化名称替换lambda表达式的语法方法,以下所有都是显式键入函数的等效定义 add :: Num a =>a->a->一个:

Combined with the syntactic sugar of replacing a lambda expression with a paramterized name, all of the following are equivalent definitions for the explicitly typed function add :: Num a => a -> a -> a:

add = \x -> \y -> x + y -- or \x y -> x + y as noted above
add x = \y -> x + y
add x y = x + y

这篇关于部分应用的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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