Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
A discriminant function in continuation passing style

Here is a possible solution:

(define (discriminant a b c k0) (square b (lambda (v1) (mult 4 a (lambda (v2) (mult v2 c (lambda (v3) (sub v1 v3 k0)))))))) (define (square a k1) (mult a a k1)) (define (mult a b k2) (k2 (* a b))) (define (sub a b k3) (k3 (- a b))) (define (plus a b k4) (k4 (+ a b)))

Please notice that this CPS variant of the program dictates a particular evaluation order:

  1. First we square b: b*b
  2. Multiply 4 and a
  3. Multiply the result of step 2 with c. Now we have 4*a*c
  4. Finally we subtract the result of step 3 from the result of step 1

After these steps - in this particular order - pass the final result b*b - 4*a*c to k0