Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          fac-direct-cps.scm - The usual recursive factorial function - in direct and continuation passing style.Lecture 3 - slide 33 : 43
Program 4

(define (fact-direct n)
  (if (= n 0)
      1
      (* n (fact-direct (- n 1)))))


(define (fact-cps n k)
  (if (= n 0)
      (k 1)
      (fact-cps (- n 1)
                (lambda(v)       ; Eventually v becomes (- n 1)!
                  (k (* n v)))   ; Now pass (* n v) = (* n  (- n 1)! ) to k.
      )
  )
)

; Notice that fact-cps is tail recursive!