方案中的延续传递风格?
我在维基百科上遇到了这段代码:
(define (pyth x y k)
(* x x (lambda (x2)
(* y y (lambda (y2)
(+ x2 y2 (lambda (x2py2)
(sqrt x2py2 k))))))))
文章说该代码是另一段代码的 Continuation-Passing 版本代码:
(define (pyth x y)
(sqrt (+ (* x x) (* y y))))
但是,我很困惑:这是如何工作的?这里如何将数字乘以 lambda? (* xx (lambda ...))
I ran into this code on Wikipedia:
(define (pyth x y k)
(* x x (lambda (x2)
(* y y (lambda (y2)
(+ x2 y2 (lambda (x2py2)
(sqrt x2py2 k))))))))
The article says that that code is the Continuation-Passing version of another piece of code:
(define (pyth x y)
(sqrt (+ (* x x) (* y y))))
However, I'm quite confused: How does that even work? How do you multiply a number by a lambda here? (* x x (lambda ...))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在维基百科示例中,
*
与传统示例中的*
含义不同。我将把维基百科的例子重写为:
在这种形式中,每个 cps-xxx 函数执行指示的操作,然后将结果传递给最后一个参数。您可以这样称呼它:
将 2 和 3 相乘,得到 6,然后将 6 传递给
display
。 (实际上,您希望将结果传递给 cps-display 来显示其初始参数,然后调用指定为最后一个参数的另一个函数)。In the Wikipedia example,
*
doesn't mean the same thing as*
in the conventional example.I would rewrite the Wikipedia example as:
In this form, each of the
cps-xxx
functions perform the operation indicated and then pass the result to the last argument. You could call it like this:which would multiply 2 and 3, giving 6, and then passing 6 to
display
. (Actually you would want to pass the result to acps-display
that displayed its initial argument(s) and then called another function specified as its last parameter).