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