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

Notes: Monday 28 January 2002
Schedule

Notes

(define color-difference
  (lambda (cf) 
   (lambda (colora colorb)
      (+ (cf (- (get-red colora) (get-red colorb)))
          (cf (- (get-green colora) (get-green colorb)))
          (cf (- (get-blue colora) (get-blue colorb)))))))

(define (closer-color? sample color1 color2)
   (< ((color-difference square) color1 sample)
       ((color-difference square) color2 sample)))
(define (fibo n)
  (if (or (= n 1) (= n 2))
      1 ;;; base case
      (+ (fibo (- n 1))
         (fibo (- n 2)))))

(define (fast-fibo n)
  (define (fibo-worker a b count)
    (if (= count 1)
        b
        (fibo-worker (+ a b) a (- count 1))))
  (fibo-worker 1 1 n))
> (require-library "trace.ss")
> (trace fibo)
> (fibo 5)
|(fibo 5)
| (fibo 4)
| |(fibo 3)
| | (fibo 2)
| | 1
| | (fibo 1)
| | 1
| |2
| |(fibo 2)
| |1
| 3
| (fibo 3)
| |(fibo 2)
| |1
| |(fibo 1)
| |1
| 2
|5
> (fast-fibo 5)
|(fast-fibo 5)
| (fibo-worker 1 1 5)
| |(fibo-worker 2 1 4)
| | (fibo-worker 3 2 3)
| | |(fibo-worker 5 3 2)
| | | (fibo-worker 8 5 1)
| | | 5
| | |5
| | 5
| |5
| 5
|5






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