如何创建像球拍一样的咖喱功能

编程入门 行业动态 更新时间:2024-10-09 03:31:36
本文介绍了如何创建像球拍一样的咖喱功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想看看如何模拟 racket 提供的 (curry func).这是我如何手动柯里化函数的示例:

I would like to see how it might be possible to emulate the (curry func) that racket provides. Here is an example of how I'm manually currying a function:

#lang sicp ; convert to a curried function (define (add1 x y) (+ x y)) (define add2 (lambda (x) (lambda (y) (+ x y))))

(add1 2 3) ; 5 ((add2 2) 3) ; 5

我从哪里开始添加高阶函数,以便将普通"函数转换为柯里化函数,如下所示:

Where would I start to add a higher-order function such that it converts a 'normal' function into a curried function, something like this:

(((curry add1) 2) 3)

推荐答案

您必须进行一些权衡,因为判断一个函数接受多少个参数并不容易.Racket 有一个 procedure-arity 函数,它让 curry 告诉有多少个参数要 curry,但 SICP 语言没有.所以你必须选择如何处理这个问题.一些合理的选择包括:

You have to make some tradeoffs because it's not easy to tell how many parameters a function accepts. Racket has a procedure-arity function that lets curry tell how many arguments to curry, but the SICP language does not. So you have to choose how to handle this. Some reasonable choices include:

  • 让调用者指定等待多少个参数
  • 仅使用固定数量的参数
  • 只对函数进行前 n 次调用,并通过底层函数进行 n+1 次调用.
  • Make the caller specify how many arguments to wait for
  • Work only with a fixed number of arguments
  • Curry only the first n calls to a function, and have the n+1th call through the underlying function.

更多推荐

如何创建像球拍一样的咖喱功能

本文发布于:2023-11-24 22:01:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627024.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:球拍   咖喱   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!