Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          list-length-cps.scm - The list-length function - continuation passing style - handling improper lists.Lecture 3 - slide 33 : 43
Program 6

; list-length in direct style - makes use of call-with-current-continuation to capture the outer continuation. 
(define (list-length-direct lst)
  (call-with-current-continuation
   (lambda (do-exit)
     (letrec ((list-length-inner
                (lambda (lst)
                   (cond ((null? lst) 0)
                         ((pair? lst) (+ 1 (list-length-inner (cdr lst))))
                         (else (do-exit 'improper-list))))))
       (list-length-inner lst)))  ))


(define (list-length-cps lst k0)    ; k0 is the outer continuation - ready to catch exceptional values
  (letrec ((list-length-inner
             (lambda (lst k1) 
               (cond ((null? lst) (k1 0))
                     ((pair? lst) (list-length-inner
                                   (cdr lst)
                                   (lambda (v) (k1 (+ 1 v))) ; v is the length of (cdr l).
                                                             ; Pass 1+v to k1.
                                )
                     )
                     (else (k0 'improper-list)))) ))         ; Pass the symbol improper-list
                                                             ; to the outer continuation k0.
    (list-length-inner lst k0)))

; The outer continuation k0 is used for 'returning' exceptional values.
; A chain of inner continations on the form (lambda (v) (k1 (+ 1 v))) accumulate the length.
; Heavy and clumsy. Next we show a much more elegant iterative version.