currying和部分应用有什么区别? [英] What is the difference between currying and partial application?

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

问题描述

我经常在Internet上看到各种抱怨,认为其他人使用的不是curying的示例,而实际上只是部分应用.

I quite often see on the Internet various complaints that other peoples examples of currying are not currying, but are actually just partial application.

我还没有找到关于什么是部分申请或它与currying有何不同的正当解释.似乎存在一个普遍的混乱,在某些地方将等效示例描述为"currying",而在某些地方将其部分应用.

I've not found a decent explanation of what partial application is, or how it differs from currying. There seems to be a general confusion, with equivalent examples being described as currying in some places, and partial application in others.

有人可以给我提供两个术语的定义,以及它们之间的区别的详细信息吗?

Could someone provide me with a definition of both terms, and details of how they differ?

推荐答案

Currying会将 n 个参数的单个函数转换为每个都具有单个参数的 n 个函数.赋予以下功能:

Currying is converting a single function of n arguments into n functions with a single argument each. Given the following function:

function f(x,y,z) { z(x(y));}

当咖喱变成:

function f(x) { lambda(y) { lambda(z) { z(x(y)); } } }

为了获得f(x,y,z)的完整应用,您需要执行以下操作:

In order to get the full application of f(x,y,z), you need to do this:

f(x)(y)(z);

许多功能语言可让您编写f x y z.如果仅调用f x y f(x)(y),则会得到部分应用的函数-返回值是lambda(z){z(x(y))}的闭包,并传入x和y至f(x,y).

Many functional languages let you write f x y z. If you only call f x y or f(x)(y) then you get a partially-applied function—the return value is a closure of lambda(z){z(x(y))} with passed-in the values of x and y to f(x,y).

使用部分应用程序的一种方法是将函数定义为广义函数的部分应用程序,例如 fold :

One way to use partial application is to define functions as partial applications of generalized functions, like fold:

function fold(combineFunction, accumulator, list) {/* ... */}
function sum     = curry(fold)(lambda(accum,e){e+accum}))(0);
function length  = curry(fold)(lambda(accum,_){1+accum})(empty-list);
function reverse = curry(fold)(lambda(accum,e){concat(e,accum)})(empty-list);

/* ... */
@list = [1, 2, 3, 4]
sum(list) //returns 10
@f = fold(lambda(accum,e){e+accum}) //f = lambda(accumulator,list) {/*...*/}
f(0,list) //returns 10
@g = f(0) //same as sum
g(list)  //returns 10

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

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