柯里化函数的实际使用? [英] Practical use of curried functions?

查看:21
本文介绍了柯里化函数的实际使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有大量关于如何使用柯里化函数的教程,以及在 stackoverflow 上的许多问题.然而,在阅读了 The Little Schemer、几本书、教程、博客文章和 stackoverflow 线程后,我仍然不知道这个简单问题的答案:柯里化有什么意义?"我确实了解如何咖喱函数,而不是为什么?"后面.

There are tons of tutorials on how to curry functions, and as many questions here at stackoverflow. However, after reading The Little Schemer, several books, tutorials, blog posts, and stackoverflow threads I still don't know the answer to the simple question: "What's the point of currying?" I do understand how to curry a function, just not the "why?" behind it.

有人可以向我解释柯里化函数的实际用途吗(在每个函数只允许一个参数的语言之外,使用柯里化的必要性当然非常明显.)

Could someone please explain to me the practical uses of curried functions (outside of languages that only allow one argument per function, where the necessity of using currying is of course quite evident.)

考虑到 TLS 的一些示例,

edit: Taking into account some examples from TLS, what's the benefit of

(define (action kind)
    (lambda (a b)
        (kind a b)))

相对于

(define (action kind a b)
    (kind a b))

我只能看到更多的代码而没有增加灵活性...

I can only see more code and no added flexibility...

推荐答案

柯里化函数的一个有效用途是减少代码量.

One effective use of curried functions is decreasing of amount of code.

考虑三个函数,其中两个几乎相同:

Consider three functions, two of which are almost identical:

(define (add a b)
  (action + a b))

(define (mul a b)
  (action * a b))

(define (action kind a b)
  (kind a b))

如果您的代码调用了 add,它又会以 + 的种类调用 action.与 mul 相同.

If your code invokes add, it in turn calls action with kind +. The same with mul.

您定义了这些函数,就像在许多可用的命令式流行语言中所做的一样(其中一些已经包括了 lambda、柯里化和函数世界中常见的其他特性,因为它们都非常方便).

You defined these functions like you would do in many imperative popular languages available (some of them have been including lambdas, currying and other features usually found in functional world, because all of it is terribly handy).

所有addsum 所做的,都是用适当的kind 包装对action 的调用.现在,考虑这些函数的柯里化定义:

All add and sum do, is wrapping the call to action with the appropriate kind. Now, consider curried definitions of these functions:

(define add-curried
  ((curry action) +))

(define mul-curried
  ((curry action) *))

它们变得相当短.我们只是通过只传递一个参数 kind 来对函数 action 进行柯里化,并得到了接受其余两个参数的柯里化函数.

They've become considerable shorter. We just curried the function action by passing it only one argument, the kind, and got the curried function which accepts the rest two arguments.

这种方法可以让您编写更少的代码,并具有高度的可维护性.

This approach allows you to write less code, with high level of maintainability.

想象一下,函数 action 会立即被重写以接受 3 个以上的参数.如果没有柯里化,您将不得不重写 addmul 的实现:

Just imagine that function action would immediately be rewritten to accept 3 more arguments. Without currying you would have to rewrite your implementations of add and mul:

(define (action kind a b c d e)
  (kind a b c d e))

(define (add a b c d e)
  (action + a b c d e))

(define (mul a b c d e)
  (action * a b c d e))

但是柯里化让你免于那些讨厌且容易出错的工作;您根本不必重写函数 add-curriedmul-curried 中的符号,因为调用函数将提供传递给的必要数量的参数动作.

But currying saved you from that nasty and error-prone work; you don't have to rewrite even a symbol in the functions add-curried and mul-curried at all, because the calling function would provide the necessary amount of arguments passed to action.

这篇关于柯里化函数的实际使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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