咖喱功能的实际使用? [英] Practical use of curried functions?

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

问题描述

有很多关于如何嵌入函数的教程,以及在stackoverflow上的许多问题。然而,在阅读The Little Schemer之后,几本书,教程,博客帖子和stackoverflow线程我仍然不知道这个简单问题的答案:currying的意义何在?我确实懂得如何咖啡功能,而不是为什么?

有人可以向我解释curried函数的实际用法(除了每个函数只允许一个参数的语言之外,其中使用currying的必要性是)



编辑:考虑到TLS的一些示例,
$的好处是什么b $ b

 (define(action kind)
(lambda(ab)
(kind ab)))

相对于

 (define(action kind ab)
(kind ab))

我只能看到更多的代码并且没有添加灵活性...

解决方案

一个有效使用curried函数的方法是减少代码量。

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

 (define(add ab)

$ b(define(mul ab)
(action * ab))

(define(actio n kind ab)
(kind ab))

如果您的代码调用 add ,它会依次以 + 的形式调用 action 。与 mul



相同。您定义这些函数的方式与您在许多必需的常用语言他们一直在包括lambdas,currying和其他功能世界中常见的功能,因为它都非常方便)。

所有 add code>和 sum do,正在使用相应的包装调用 action 。现在,考虑一下这些函数的curried定义:

$ $ $ $ $ $ $ $ $ $($)

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

他们变得更短了。我们通过只传递一个参数类型来检查函数 action ,并得到了curried函数,它接受休息两个参数。

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



只是想象一下,函数 action 会立即被重写为接受3个参数。如果不加currying,你必须重写 add mul 的实现:

 (define(action kind abcde)
(kind abcde))

(define(add abcde)
(action + abcde))

(define(mul abcde)
(action * abcde))

但柯里克将你从那些讨厌和容易出错的工作中拯救出来;你根本不必重写函数 add-curried mul-curried 中的一个符号,因为调用函数将提供必要数量的参数传递给 action


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.)

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

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

as opposed to

(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))

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

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).

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) *))

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.

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))

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天全站免登陆