Scheme의 연속 지나가는 스타일?
-
15-11-2019 - |
문제
이 코드를 실행했습니다 Wikipedia :
(define (pyth x y k)
(* x x (lambda (x2)
(* y y (lambda (y2)
(+ x2 y2 (lambda (x2py2)
(sqrt x2py2 k))))))))
.
해당 코드는 다른 코드의 다른 코드의 연속 지나가는 버전입니다.
(define (pyth x y)
(sqrt (+ (* x x) (* y y))))
.
그러나 나는 꽤 혼란스러워합니다. 어떻게 작동합니까?여기서 람다로 숫자를 어떻게 괴롭 히니?(* 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:
(define (pyth x y k)
(cps-* x x (lambda (x2)
(cps-* y y (lambda (y2)
(cps-+ x2 y2 (lambda (x2py2)
(cps-sqrt x2py2 k))))))))
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:
(pyth 2 3 display)
which would multiply 2 and 3, giving 6, and then passing 6 to display
. (Actually you would want to pass the result to a cps-display
that displayed its initial argument(s) and then called another function specified as its last parameter).
제휴하지 않습니다 StackOverflow