University of Virginia, Department of Computer Science
CS200: Computer Science, Spring 2002

Notes: Wednesday 27 February 2002
Schedule

Code: countletters.ss

Notes

Mutation Procedures:

Why is mutation scary?












Why is mutation useful?














Why do our evaluation rules fail when we allow mutation?










Would it be possible to create a circular list without using set-cdr!?










How would you count letters without using mutation?

(define (make-letter-tallys)
  (for (char->integer #\a) 
       (char->integer #\z) 
       (lambda (accum cno) 
         (append accum (list (cons (integer->char cno) 0))))
       null))
                                           
(define (count-letters msg)
  (let ((letter-uses (make-letter-tallys)))
    (map (lambda (c) 
           (insertlg 
            (lambda (thisletter foundit)
              (if foundit foundit 
                  (if (eq? (car thisletter) c) 
                      (begin (set-cdr! thisletter (+ (cdr thisletter) 1)) #t)
                      #f)))          
            letter-uses
            #f))
         (string->list msg))
    letter-uses))

CS 655 University of Virginia
Department of Computer Science
CS 200: Computer Science
David Evans
evans@cs.virginia.edu
Using these Materials