问题描述
当我执行以下操作时会发生什么?
What happens when I do the following?
(define ((func x) y)
(if (zero? y)
((func x) 1)
12))
我知道我可以做到这一点:
I understand that I can do this:
(define curried (func 5))
现在我可以使用咖喱了.我很好奇的是函数的定义.是否行
And now I can use curried. What I'm curious about is in the definition of the function. Does the line
((func x) 1)
以 x 作为参数创建一个新的 lambda,然后在 1 上调用它?或者它比那更聪明,它只是重新使用现有的.(例如,如果我执行 (curried 0)
,((func x) 1)
行将等价于 (curried 1)
- PLAI Scheme 会这样做吗?)
create a new lambda with x as the argument, and then invoke it on 1? Or is it smarter than that and it just re-uses the existing one. (For example, if I do (curried 0)
, the ((func x) 1)
line would be equivalent to (curried 1)
- does PLAI Scheme do this?)
推荐答案
Scheme 标准中规定
In the Scheme standard it is specified that
(define (f x) 42) is short for (define f (lambda (x) 42)) .
自然(非标准)概括意味着:
The natural (non-standard) generalization implies:
(define ((f x) y) (list x y)) is short for (define (f x) (lambda (y) (list x y)))
which is short for (define f (lambda (x) (lambda (y) (list x y))))
为了测试它,让我们试试 DrScheme 中的例子
To test it, let's try the example in DrScheme
欢迎使用 DrScheme,版本 4.1.3.3-svn5dec2008 [3m].语言:模块;内存限制:384 MB.
Welcome to DrScheme, version 4.1.3.3-svn5dec2008 [3m].Language: Module; memory limit: 384 megabytes.
(define ((f x) y) (list x y))(f 1)
((f 1) 2)(1 2)
((f 1) 2) (1 2)
如果我们命名临时值,可能更容易看到发生了什么:
If we name the temporary value, it might be easier to see what happens:
(定义 h (f 1))(h 2)(1 2)(h 3)(1 3)
由于PLAI Scheme"是在 DrScheme 中实现的,我相信它继承了这个快捷方式.
Since "PLAI Scheme" is implemented in DrScheme, I believe it inherits this shortcut notation.
这篇关于Scheme中柯里化函数的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!