柯里化函数在Scheme中的实现
当我执行以下操作时会发生什么?
(define ((func x) y)
(if (zero? y)
((func x) 1)
12))
我知道我可以这样做:
(define curried (func 5))
现在我可以使用柯里化。 我很好奇的是函数的定义。 该行是否
((func x) 1)
创建一个以 x 作为参数的新 lambda,然后在 1 上调用它? 或者它比这更聪明,它只是重复使用现有的。 (例如,如果我执行 (curried 0)
,则 ((func x) 1)
行将相当于 (curried 1)
- PLAI计划可以做到这一点吗?)
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)
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?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在Scheme标准中指定了
自然(非标准)泛化意味着:
中的示例
为了测试它,让我们尝试DrScheme欢迎使用DrScheme版本4.1.3.3-svn5dec2008 [3m] 。
语言:模块; 内存限制:384 MB。
如果我们命名临时值,可能会更容易看出会发生什么:
由于“PLAIScheme”是在DrScheme中实现的,我相信它继承了这个快捷符号。
In the Scheme standard it is specified that
The natural (non-standard) generalization implies:
To test it, let's try the example in DrScheme
Welcome to DrScheme, version 4.1.3.3-svn5dec2008 [3m].
Language: Module; memory limit: 384 megabytes.
If we name the temporary value, it might be easier to see what happens:
Since "PLAI Scheme" is implemented in DrScheme, I believe it inherits this shortcut notation.
我已经很久没有使用计划了,但你可能会发现这篇文章< /a> 有帮助。
它描述了两个宏 c-lambda 和 c-define 的实现,这两个宏允许 lambda 表达式的隐式柯里化定义。
It's been too long since I worked with scheme, but you might find this article helpful.
It describes the implementation of two macros, c-lambda and c-define which allow implicit curried definitions of lambda expressions.
soegaard的答案是正确的——这是传统的展开。 不过,drscheme 很聪明!
我发现以下代码在运行时间上是等效的:
原始来源:
尝试优化:
大量使用此函数(多次,而不是一次)的代码对于两个版本都以 1800 毫秒运行。 更有趣的是,这个版本(我对所发生情况的可视化):
运行时间为 2000 毫秒。 因此,如果对替换内替换的调用每次都创建一个 lambda,那么速度肯定会变慢,但快捷表示法似乎并非如此。
soegaard's answer is correct - this is the traditional expansion. However, drscheme is smart!
The following code I've found to be equivalent in running time:
Original source:
Attempt at optimization:
Code which heavily uses this function (multiple times, not just once) runs at 1800 ms for both versions. More interestingly, this version (my visualization of what was happening):
Runs at 2000 ms. So there is definitely a slow-down if the calls to substitute within substitute were each creating a lambda, but it appears this is not the case with the shortcut notation.