[plt-scheme] CPS conversion example, question

From: Grant Rettke (grettke at acm.org)
Date: Wed Jan 9 16:46:56 EST 2008

Hi folks,

Today I was reading the Wikipedia article:


on continuation passing style. It has some example written in Scheme,
though I'm not sure if they are meant to run or not. In the example of
converting the "pyth" function from direct style to cps style, for
example, the direct style runs, but the cps style seems to be written

Nonetheless I wanted to try converting a running example from direct
style to cps style, and did so by relying on the fact that let can be
implemented in terms of lambda using a conversion pattern. Here are
the steps I took:

;; Direct style
(define (pyth-ds x y)
    (sqrt (+ (* x x) (* y y))))

;; Let style
(define (pyth-ls x y k)
    (let ([x2 (* x x)])
      (let ([y2 (* y y)])
        (let ([x2py2 (+ x2 y2)])
          (k (sqrt x2py2))))))

; CPS Style
(define (pyth-cps x y k)
    ((λ (x2)
       ((λ (y2)
          ((λ (x2py2)
             (k (sqrt x2py2))) (+ x2 y2))) (* y y))) (* x x)))

Is that the right way to do it?

"Wisdom begins in wonder!"

Posted on the users mailing list.