Lecture overview -- Keyboard shortcut: 'u'  Previous page: Use of continuations for escaping purposes -- Keyboard shortcut: 'p'  Next page: Practical example: Searching a binary tree -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home    Lecture 3 - Page 41 : 42
Functional Programming in Scheme
Name binding, Recursion, Iteration, and Continuations
Practical example: Length of an improper list

The length of an improper list is undefined

We chose to return the symbol improper-list if list-length encounters an improper list

This example is similar to the catch and throw example shown earlier in this section

(define (list-length l)
  (call-with-current-continuation
   (lambda (do-exit)
     (letrec ((list-length1
                (lambda (l)
                   (cond ((null? l) 0)
                         ((pair? l) (+ 1 (list-length1 (cdr l))))
                         (else (do-exit 'improper-list))))))
       (list-length1 l)))  ))

The function list-length, which returns the symbol 'improper-list in case it encounters an improper list.