Continuation Passing Style

April 3, 2018

Following the Wikipedia article, we give the continuation passing style version of a function to compute the hypotenuse of a right triangle using the Pythagorean theorem; here are normal and continuation passing style versions:

(define (pyth x y)
  (sqrt (+ (* x x) (* y y))))
(define (pyth& x y k)
  ((cps *) x x (lambda (x2)
                 ((cps *) y y (lambda (y2)
                                ((cps +) x2 y2 (lambda (x2+y2)
                                                 ((cps sqrt) x2+y2 k))))))))
> (pyth& 3 4 (lambda (x) x))
5

You can run the program at https://ideone.com/kPFH79.

Advertisements

Pages: 1 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: